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
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.
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.
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.
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 :)
$('p').not(":nth-child(1)").not(":nth-child(2)")
-- SEE DEMO --
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With