I tried this but it doesn't work:
$('div').add($(this).siblings());
I want to animate the siblings simultaneously. Can anyone help?
jQuery siblings() MethodThe siblings() method returns all sibling elements of the selected element. Sibling elements are elements that share the same parent. The DOM tree: This method traverse forward and backwards along siblings of DOM elements.
The ("element ~ siblings") selector selects sibling elements that appear after the specified "element". Note: Both of the specified elements must share the same parent.
The siblings() is an inbuilt method in jQuery which is used to find all siblings elements of the selected element. The siblings are those having same parent element in DOM Tree.
Answer: Use the jQuery find() Method You can use the find() method to get the children of the $(this) selector using jQuery. The jQuery code in the following example will simply select the child <img> element and apply some CSS style on it on click of the parent <div> element.
You need to use .addBack()
to get both the div and its siblings. In jQuery versions earlier than 1.8, you should use .andSelf()
instead, which is now just an alias for .addBack()
(thanks Sable).
// jQuery 1.8+ $("#myDiv").siblings().addBack(); // jQuery <1.8 $("#myDiv").siblings().andSelf();
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