jQuery Event How to - Prevent default event firing but still allow event to bubble








Question

We would like to know how to prevent default event firing but still allow event to bubble.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from  w  ww.j a  v  a 2  s.  co  m-->
    $('a').click(function(e){
         e.preventDefault();
    
    });
    $('div').click(function(e){
         console.log('bubbled');
    
    });
});
</script>
</head>
<body>
  <div>
    <a href="">Test</a>
  </div>
</body>
</html>

The code above is rendered as follows: