Preventing Default Operation in Javascript


Google
You can stop HTML Elements default operation by using the same addEventListener() method. For Example, you want to add click event to a hyperlink. But, as soon as you click on the hyperlink it follows the link you may or may not specified in href attribute. So, to prevent default operation of this hyperlink we use preventDefault() method.

HTML

<a href="dlalpha.wordpress.com" id="homepage">Destructive Labs Alpha</a>

JavaScript

var homepageLink = document.getElementById("homepage");

homepageLink.addEventListener("click",ChangeLinkText,false);

function ChangeLinkText(e){

homepageLink.text = "DL Alpha";

e.preventDefault();

}
Advertisements

One thought on “Preventing Default Operation in Javascript

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s