The Muffin Man
The Muffin Man

Reputation: 20004

Help with jQuery :last selector

I have a structure like so.

<ul id="comment_list">
 <li><ul><li></li></ul></li>
 <li></li>
 <li></li>
</ul>

For brevity I left out the nested ul's in each li tag... I'm trying to remove the last li in the comment_list, what's happening is it is getting removed along with the last li in each nested ul. How do I stop that?

I have this:

$("#comment_list li:last-child").remove();

Upvotes: 0

Views: 234

Answers (3)

wilsonpage
wilsonpage

Reputation: 17610

':last-child' is different to ':last'

':last-child' is a group selector that will select multiple children that match the selector inside the parent. ':last' will only ever pick 'the last' element that matches the selector.

Try this code instead:

$("div#comment_list li:last").remove();

Hope that helps you bro!

W.

Upvotes: 0

Matt Gibson
Matt Gibson

Reputation: 38238

Try $("#comment_list > li:last-child").remove();

Upvotes: 3

simshaun
simshaun

Reputation: 21466

Try $("#comment_list > li:last-child").remove();

Upvotes: 3

Related Questions