user1224129
user1224129

Reputation: 2779

Skip first N elements in JQuery

I would like to know, how can I skip first N elements in JQuery. Something like this:

<div id="test">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    ...
</div>

$('#test > div').skip(2)

Should return

<div>3</div>
<div>4</div>
...

I know I can just use :not(:first-child):not(:first-child + div)... selector N times, but is there a better way?

Upvotes: 26

Views: 27949

Answers (4)

Baltazar Zaraik
Baltazar Zaraik

Reputation: 31

Skip just the first one - example:

$("#spaccordion li:gt(0)").addClass("collapsed");

All <li> items will have class "collapsed" except the first one

Upvotes: 1

lordvlad
lordvlad

Reputation: 5347

Use the .slice() function, it gives you the subset of elements based on its index.

$('#test > div').slice( 2 )

Reference: http://api.jquery.com/slice/

Upvotes: 10

Sani Huttunen
Sani Huttunen

Reputation: 24385

jQuery has a gt selector. (Greater than).

$('#test > div:gt(1)')

Or you can use the slice function

$('#test > div').slice(2)

Upvotes: 56

jokeyrhyme
jokeyrhyme

Reputation: 3638

I think you are looking for the :gt selector: http://api.jquery.com/gt-selector/ Note that you start counting from 0 here.

Try:

$('#test > div:gt(1)')

Upvotes: 2

Related Questions