user3065191
user3065191

Reputation: 145

jQuery two different delegates

I have two delegate functions working..but I want that if the img is clicked avoid the click on the <li>.

<li><img src='img.png'/></li>

$('#listview').delegate('li img', 'click', function(){
    alert('img clicked!');
}); 
$('#listview').delegate('li', 'click', function() { 
    window.open("details.php?id" + $(this).attr('id'), '_blank');
}); 

If the image (inside the li) is clicked I would like NOT to open that page details. Currently, when I click the image it alerts plus show the new page..how can I avoid that?

Upvotes: 0

Views: 51

Answers (1)

Felix
Felix

Reputation: 38102

Try using event.stopImmediatePropagation():

$('#listview').delegate('li img', 'click', function(e){
    e.stopImmediatePropagation();
    alert('img clicked!');
}); 

to prevent event buble up the DOM tree.

Upvotes: 3

Related Questions