Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery steals click from children

Tags:

jquery

Say i have

<h3>
  some text
  <a href="google.com">google</a>
</h3>

I want to attach a click event to the h3

$("h3").click(function(){ $(this).slideDown(); return false; });

but I also want to preserve the clicking on the actual link. Is there a way to do this with jQuery?

Thanks!

like image 656
Silviu Postavaru Avatar asked Oct 20 '10 00:10

Silviu Postavaru


2 Answers

You can check the event's actual .target and do nothing if it was an <a> element, for example:

$("h3").click(function(e){ 
  if(e.target.nodeName == 'A') return;
  $(this).slideDown(); 
  return false; 
});
like image 117
Nick Craver Avatar answered Oct 24 '22 05:10

Nick Craver


Just don't prevent the default behavior:

$("h3").click(function(){ $(this).slideDown(); });

Also, do you mean .slideUp()? How can you click on something before it slides down?

Try it out with this jsFiddle

like image 33
Peter Ajtai Avatar answered Oct 24 '22 06:10

Peter Ajtai