Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery selector - exclude first and second

I want to exclude first and second element from jquery selector. I tried this:

$('p:not(:nth-child(1),:nth-child(2))')

But this only excludes the first element... Thanks

like image 666
simPod Avatar asked May 11 '12 08:05

simPod


People also ask

How do you select all child elements except first?

By using the :not(:first-child) selector, you remove that problem. You can use this selector on every HTML element. Another common use case is if you use an unordered list <ul> for your menu.

How to select nth child in jQuery?

jQuery :nth-child() SelectorThe :nth-child(n) selector selects all elements that are the nth child, regardless of type, of their parent. Tip: Use the :nth-of-type() selector to select all elements that are the nth child, of a particular type, of their parent.


3 Answers

This jQuery sequence uses .slice() to discard the first two elements from the $('p') selector:

$('p').slice(2).

see http://jsfiddle.net/alnitak/zWV7Z/

Note that this is not the same as nth-child - the exclusion is based on the whole set of elements found in the first selector, and not on their relative position in the DOM.

like image 180
Alnitak Avatar answered Oct 19 '22 10:10

Alnitak


Simply:

$('p:gt(1)')

http://api.jquery.com/gt-selector/

Demo http://jsfiddle.net/NtFYq/1/

As Alnitak has pointed out in the comment, if performance is a concern, you can use his solution of slice

Thanks a lot @Alnitak for pointing that out :)

like image 19
Andreas Wong Avatar answered Oct 19 '22 10:10

Andreas Wong


 $('p').not(":nth-child(1)").not(":nth-child(2)")

-- SEE DEMO --

like image 5
Curtis Avatar answered Oct 19 '22 10:10

Curtis