Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 700
user1224129 Avatar asked Mar 05 '13 00:03

user1224129


3 Answers

jQuery has a gt selector. (Greater than).

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

Or you can use the slice function

$('#test > div').slice(2) 
like image 158
Sani Singh Huttunen Avatar answered Oct 11 '22 21:10

Sani Singh Huttunen


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/

like image 45
lordvlad Avatar answered Oct 11 '22 21:10

lordvlad


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)')
like image 23
jokeyrhyme Avatar answered Oct 11 '22 20:10

jokeyrhyme