views:

246

answers:

1

I have a div with a link inside of it:

<div id="myDiv">
    <a href="http://www.lol.com"&gt;Lol&lt;/a&gt;
</div>

Clicking the <div /> should go somewhere, but clicking the child <a /> should go to www.lol.com. I've seen from previous questions and the jQuery website that .stopPropagation prevents bubbling upwards, but how do I prevent a bubble downwards (isn't that what's necessary here?).

+1  A: 
Geoff
I see, thank you!
Jasie
Even though it will work, I respectfully disagree with this answer. As I said in my answer, you only need to check the `event.target` is the `<div>` element in it's click handler (using an `if` statement). There's no need to add another event handler to the `<a>` element, it's extra handlers and extra work where it's not necessary.
Andy E
Yes, you can do if( $(event.target).is( "a" ) ) { /* code here */ } inside the div event handler. It was more an explanation as to how event bubbling works. There are instances where it might be easier or more efficient to add a couple of event handlers to the children, especially if there are multiple element types that would make the if condition more complicated. I shall include it in the answer.
Geoff