How to cancel jQuery event

event.preventDefault()

The following code cancel the default action by using the preventDefault method.


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--from   w w  w  .j ava  2 s.c o m-->
              $("adiv").live("click", function(event){
                   event.preventDefault();
               });
        });
    </script>
  </head>
  <body>
    <body>
       <div><h1>header 1</h1></div>
    </body>
</html>

Click to view the demo

event.stopPropagation()

To Stop only an event from bubbling by using the stopPropagation method.


$("form").bind("submit", function(event){
  event.stopPropagation();
});

Return false to cancel event

The following code returns false to Cancel a default action and prevent it from bubbling up.


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--from w ww . j  a  v  a 2s.c  o  m-->
              $("div").live("click", function() { return false; })
        });
    </script>
  </head>
  <body>
    <body>
       <div><h1>header 1</h1></div>
    </body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities