Here!...">

Cancel spouting on HTML anchor tag

I have the following setting on my web page.

<div id="clickable">
  <a href="hello.com">Here!</a>
</div>

Is there a way I could use to avoid the click event to trigger the div. I suppose this is due to setting something in the attribute onclickfor the anchor tag, but trying simple things like this e.preventDefault()did not work.

Help? Thank!

+5
source share
4 answers

e.preventDefault (); do not work in onclick attributes because e is not defined. Also e.preventDefault (); don't want you to stop bubbling, you need e.stopPropagation ();

Use either

onclick="$(this).stopPropagation();"

anchored or

$(a).click(function(e){
  e.stopPropagation();
});

in your events.

+6
source

e.preventDefault() . e.stopPropagation() return false.

.

+3

You can use onclick = "return false;"

<a href="#" onclick="return false;">stuff</a>

+2
source

You can return false;or make sure your function uses eas an argument

$("#clickable a").click(function(e){
   //stuff
   e.preventDefault;
});
0
source

All Articles