jQuery > Events

Stop default behavior in jQuery

How to stop the default behavior of an html element in jQuery?


To prevent the default behavior of an element, preventDefault() method can be used.

<a id="aPreventDefault" href="http://www.itfunda.com">preventDefault() - Ideally it should go to Attributes.aspx page</a>

 <script>
     $("#aPreventDefault").click(function (e) {
         e.preventDefault();
         $("#div3").text("sorry default behavior is prevented");
     });
 </script>
    

In the above code snippet, when the anchor <a> tag will be clicked the user should be redirected to http://www.itfunda.com but he will not be redirected as we have attached click event in the anchor <a> tag and clicking this anchor tag calls funciton in which we have called “preventDefault()” method that prevents the default behavior of the anchor tag and write "sorry default behavior is prevented" message in the div whose id is "div3".

 Views: 5062 | Post Order: 68



Write for us






Hosting Recommendations