user1680833
user1680833

Reputation: 1

Returning a collection of specific elements using jQuery

How can I get all li elements starting from second item (level_2) onwards using jQuery?

<ul>
    <li class="item level_1">text1</li>
    <li class="item level_2">text2</li>
    <li class="item level_3">text3</li>
    <li class="item level_4">text4</li>
    <li class="item level_3">text5</li>
    <li class="item level_2">text2</li>
    <li class="item level_3">text3</li>
</ul>

Upvotes: 0

Views: 55

Answers (5)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

I prefer using .slice.

var $li = $('ul li');
var $li_from_second = $li.slice(1);

Upvotes: 0

adeneo
adeneo

Reputation: 318182

You can use the :gt() selector and select all li with an index greater than zero :

$('li:gt(0)')

Upvotes: 0

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123377

Use nth-child:

$('li.item:nth-child(n+1)');

Upvotes: 0

Korvin Szanto
Korvin Szanto

Reputation: 4501

There are several ways to filter a list of elements, in this case .not will work well for you.

$('li').not(':first-child');

Upvotes: 1

Interrobang
Interrobang

Reputation: 17434

Look into using nextAll().

$("li").eq(0).nextAll("li");

This will select all <li>s starting with the second one.

Upvotes: 2

Related Questions