In the context of this exercise, the
.siblings() method returns all siblings of an element, but how can we target specific sibling elements?
There are a few ways you can target specific sibling elements when utilizing the
One way is to pass in a selector, like a class name, such that only the siblings that match it will be returned. For example, the following will only return sibling elements with the class “target-class”.
Another way you can target specific sibling elements is by selecting them one at a time, by index from the jQuery object containing all the siblings. For example, given the HTML code,
<li>Item 1</li> <li class="target">Item 2</li> <li>Item 3</li> <li>Item 4</li>
We could select the first and last sibling elements by doing the following,
var siblings = $(".target").siblings(); console.log(siblings); // <li>Item 1</li> console.log(siblings); // <li>Item 4</li>