Select all children except the first

Say I have the following:

<ul> <li>First item</li> <li>Second item</li> <li>Third item</li> </ul> 

How to select all children after the first using jQuery? So I can achieve something like:

 <ul> <li>First item</li> <li class="something">Second item</li> <li class="something">Third item</li> </ul> 
+72
javascript jquery
Oct 07 '08 at 13:21
source share
7 answers

You should be able to use the "not" and "first child" selectors.

 $("li:not(:first-child)").addClass("something"); 

http://docs.jquery.com/Selectors/not

http://docs.jquery.com/Selectors/firstChild

+125
Oct 07 '08 at 13:25
source share
β€” -

Based on my completely unscientific analysis of the four methods here, it seems that there is not much speed difference between them. I ran each page containing a series of unordered lists of various lengths and confined to them using the Firebug profiler.

 $("li").slice(1).addClass("something"); 

Average time: 5,322ms

 $("li:gt(0)").addClass("something"); 

Average time: 5.590ms

 $("li:not(:first-child)").addClass("something"); 

Average time: 6.084ms

 $("ul li+li").addClass("something"); 

Average time: 7.831ms

+30
Oct 09 '08 at 12:26
source share

http://docs.jquery.com/Traversing/slice

 $("li").slice(1).addClass("something"); 
+13
Oct 07 '08 at 13:24
source share

More elegant query (select all li elements preceded by li element):

$('ul li+li')

+9
Oct 07 '08 at 13:45
source share

This should work

 $('ul :not(:first-child)').addClass('something'); 
+2
07 Oct '08 at 13:29
source share

I would use

 $("li:gt(0)").addClass("something"); 
+2
Oct 07 '08 at 14:31
source share

This is what I still work.

 $('.certificateList input:checkbox:gt(0)') 
+1
Oct 07 '08 at 13:27
source share



All Articles