preventDefault() Event Method - Javascript DOM

Javascript examples for DOM:Event

Description

The preventDefault() method cancels the cancelable event.

To stop propagation of an event through the DOM, use the stopPropagation() method.

Parameters

None

Return Value

No return value

The following code shows how to Prevent a link from opening the URL:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<a id="myAnchor" href="http://java2s.com">Visit java2s.com</a>

<script>
document.getElementById("myAnchor").addEventListener("click", function(event){
    event.preventDefault()/*w w  w  . j av  a  2  s . co  m*/
    console.log("Was preventDefault() called: " + event.defaultPrevented);
});
</script>

</body>
</html>

Related Tutorials