Say I have the following:
<ul> <li>First item</li> <li>Second item</li> <li>Third item</li> </ul>
How would I select all the child elements after the first one using jQuery? So I can achieve something like:
<ul> <li>First item</li> <li class="something">Second item</li> <li class="something">Third item</li> </ul>
To select all the children of an element except the last child, use :not and :last-child pseudo classes.
The trick is very easy, in CSS we have the sibling selector ("+"), if i will make selector that choose "li + li" it will select all list-items except the first . That's all!
Use the :not(selector) Selector Not to Select the First Child in CSS. We can use the :not(selector) selector to select every other element that is not the selected element. So, we can use the selector not to select the first child in CSS.
You should be able to use the "not" and "first child" selectors.
$("li:not(:first-child)").addClass("something");
http://docs.jquery.com/Selectors/not
http://docs.jquery.com/Selectors/firstChild
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