i try to block the jq click() when you click on an link in a div.
HTML
<div id="all">
<div id="test">
<a href="http://google.de">google</a>
</div>
</div>
JS
$('#test').click(function() { alert('only when i click the div'); });
$('a').click(function(e) {
e.stopPropagation();
e.preventDefault();
$('body').append(e.target.href);
});
This code works great but my content is dynamically so i need a delegate() solution.
The code below dont work. But why? Whats the problem?
$('#all').delegate("a", "click", function(e)
{
e.stopPropagation();
e.preventDefault();
$('body').append(e.target.href);
});
example
http://jsfiddle.net/Lf3hL/13/
stopPropagation doesn’t work with delegate – http://api.jquery.com/event.stopPropagation/
Now if you change your test click to use a delegate and use stopImmediatePropagation it will work
http://jsfiddle.net/Lf3hL/14/