Select all child elements except the first

JavascriptJquery

Javascript Problem Overview


Say I have the following:

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

How would I select all the child elements after the first one 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>

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

Based on my totally unscientific analysis of the four methods here, it looks like there's not a lot of speed difference among them. I ran each on a page containing a series of unordered lists of varying length and timed 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

Solution 3 - Javascript

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

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

Solution 4 - Javascript

A more elegant query (select all li elements that are preceded by a li element):

$('ul li+li')

Solution 5 - Javascript

This should work

$('ul :not(:first-child)').addClass('something');

Solution 6 - Javascript

i'd use

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

Solution 7 - Javascript

This is what I have working so far

$('.certificateList input:checkbox:gt(0)')

Solution 8 - Javascript

Use jQuery .not() method

$('li').not(':first').addClass('something');

OR

var firstElement = $('li').first();
$('li').not(firstElement).addClass('something');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris CanalView Question on Stackoverflow
Solution 1 - JavascripttwerntView Answer on Stackoverflow
Solution 2 - JavascripttwerntView Answer on Stackoverflow
Solution 3 - JavascriptJonny BuchananView Answer on Stackoverflow
Solution 4 - JavascriptSergey IlinskyView Answer on Stackoverflow
Solution 5 - JavascriptPatView Answer on Stackoverflow
Solution 6 - JavascriptignuView Answer on Stackoverflow
Solution 7 - JavascriptChris CanalView Answer on Stackoverflow
Solution 8 - Javascriptuser6570342View Answer on Stackoverflow