How to prevent default event firing but still allow event to bubble

Steve picture Steve · Jul 19, 2011 · Viewed 10.5k times · Source

Using jQuery: with the following code I'd like to prevent the href url (in this case a hash '#') being fired on click, but still allow the click event to continue bubbling up the chain. How can this be achieved please?

<div>
    <a href="#">Test</a>
</div>

$('a').click(function(e){
    // stop a.click firing but allow click event to continue bubbling?
});

Answer

Shef picture Shef · Jul 19, 2011
$('a').click(function(e){
    e.preventDefault();
    // stop a.click firing but allow click event to continue bubbling?
});

e.preventDefault() won't prevent bubbling, e.stopPropagation() or return false (stop both) will.