tags:

views:

64

answers:

2

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!

+5  A: 

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; 
});
Nick Craver
+4  A: 

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

Peter Ajtai
The actions inside are irrelevant. Mr. Craver nailed the thing :)
Silviu Postavaru
@Silviu - This method works too, and it's more concise if you don't need the `return false;` for some other reason.
Peter Ajtai
The thing is that I don't want to click on the <a> and have the <h3> event triggered. My bad for not giving enough details. Still upvoted you, as your answer is correct.
Silviu Postavaru
@Silviu - I see. Yes, in that case you have to use Nick's method.
Peter Ajtai