preventDefault() Event Method - Javascript DOM

Javascript examples for DOM:Event


The preventDefault() method cancels the cancelable event.

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



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>

<a id="myAnchor" href="">Visit</a>

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);


Related Tutorials