My DOM looks something like this:
<li>
<li><a class="editEntity>Edit</a></li>
<li><a class="deleteEntity>Delete</a></li>
</li>
When the used clicks on ‘Edit’, I want to change the outer <li> to <li class="selected>.
I tried something like this, but this is not working:
$('li a.editEntity').live('click', function() {
$(this).closest('li').closest('li').addClass('selected');
});
Any help is appreciated.
Go up a parent:
It wasn’t working because
closeststarts with the current element, and so if you call it on something that matches the selector, you get back the same thing you started with.Live example
Or you can use
parentswith the:eqselector:Note that
:equses 0-based indexes, so:eq(1)is the second parentli.Live example
Your quoted HTML is invalid, though (an
lican’t directly contain anli); I assume you meant:…or similar.