Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get child elements from a parent but not first and last

I would like to know how could I write a jQuery selector that get all children from a parent element except first and last child?

Example of my current HTML:

<div id="parent">
    <div>first child( i don't want to get)</div>
    <div>another child</div>
    <div>another child</div>
    <div>another child</div>
    (...)
    <div>another child</div>
    <div>another child</div>
    <div>last child (i dont want to get neither)</div>
</div>
like image 401
Cleiton Avatar asked Jan 27 '26 02:01

Cleiton


2 Answers

Like this:

$('#parent > div:not(:first, :last)');
like image 142
treeface Avatar answered Jan 28 '26 15:01

treeface


You can do this:

$('#parent div').not(':first').not(':last')

Or

$('#parent').children().not(':first').not(':last')

Here not method will filter out first and last elements from the selector.

More Information:

  • http://api.jquery.com/not/
like image 31
Sarfraz Avatar answered Jan 28 '26 16:01

Sarfraz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!