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?

jQuery has a gt selector. (Greater than).

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

Or you can use the slice function

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

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/

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)')

Skip just the first one – example:

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

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


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   Can you write async tests that expect toThrow?

Similar Posts