views:

30

answers:

1

Hi,

I'm generating a bunch of "li" items as a result of an ajax call. They look like:

<li>
  <p>Hello result 1</p>
  <button>remove</button>
</li>    
<li>
  <p>Hello result 2</p>
  <button>remove</button>
</li>
...

can I create a single click handler that will get called when any one of the "li" item "button" elements gets clicked, and will also stop propagating the click event up to the "li" parent? Something like:

function btnClicked() {
    var liItem = this.parent; // ideally is the <li> whose button was clicked.
    return true; // don't let click event go up to <li> parent?
}

----------------- Edit ---------------------

Also, I'm not sure if I'm going to use the button element, might use a clickable div as a button instead - the same techniques should work for both though, right?

Thank you

+1  A: 
$('li').click(function(event) {
//do something
event.stopPropagation()
});

This will work in jQuery. You will want to change the li to select only the li items you want.

Aaron Harun
Ok cool, yeah how do I change the li to select only the li items I want? I mean should it be something like: $('#myList li') ?? Meaning 'restrict only to the child li items of ul with id = 'myList'
That's exactly what it would be. =)
Aaron Harun
Ok that does work, I just can't get it to work with the other handlers I already have on my page for the parent items. Will post as a new Q, thank you.