defaultPrevented Event Property - Javascript DOM

Javascript examples for DOM:Event

Description

The defaultPrevented event property checks if the preventDefault() method was called for the event.

Return Value

A Boolean, telling if the preventDefault() method was called for the event.

The following code shows how to Prevent a link from opening the URL, and check if preventDefault() was called:

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()/*  ww w  .  j av  a 2 s . c  om*/
    console.log("Was preventDefault() called: " + event.defaultPrevented);
});
</script>

</body>
</html>

Related Tutorials