Cancel 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  a va 2  s .  c  om-->
              $("adiv").live("click", function(event){
                   event.preventDefault();
               });
        });
    </script>
  </head>
  <body>
    <body>
       <div><h1>header 1</h1></div>
    </body>
</html>

Click to view the demo

The code above generates the following result.

Cancel event

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(){<!-- ww  w . j  a  v a  2s .c  om-->
              $("div").live("click", function() { return false; })
        });
    </script>
  </head>
  <body>
    <body>
       <div><h1>header 1</h1></div>
    </body>
</html>

Click to view the demo

The code above generates the following result.

Cancel event
Home »
  Javascript Tutorial »
    jQuery Reference »
      Event
Javascript Tutorial Event
Event
Event attributes
event.keycode
event.clientX/clientY
pageX/pageY
Cancel event
Event target
bind
.blur()
.change()
.click()
.error()
.dblclick()
.delegate()
.die()
.focus()
.hover()
.keydown()
.keypress()
.keyup()
.live()
.load()
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseover()
.mouseout()
.mouseup()
.off()
.on()
.one()
.ready()
.resize()
.scroll()
.select()
.submit()
.toggle()
.trigger()
.triggerHandler()
.unbind()
.undelegate()
.unload()