Steve
Steve

Reputation: 3663

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

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?
});

Upvotes: 9

Views: 11253

Answers (3)

Vivek
Vivek

Reputation: 11028

try this..

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

});

here you can find difference between return false and e.preventDefault();

Upvotes: 0

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

You could do:

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

});

fiddle here http://jsfiddle.net/tU53v/

Upvotes: 0

Shef
Shef

Reputation: 45589

$('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.

Upvotes: 9

Related Questions