Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add all siblings of the current selector in jQuery

Tags:

I tried this but it doesn't work:

$('div').add($(this).siblings()); 

I want to animate the siblings simultaneously. Can anyone help?

like image 930
luksak Avatar asked Feb 04 '11 09:02

luksak


People also ask

How can I find siblings in jQuery?

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.

Which selector selects all sibling HTML 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.

What does this siblings do in jQuery?

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.

How do you get the children of the $( this selector?

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.


1 Answers

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(); 
  • http://api.jquery.com/addBack/
  • http://api.jquery.com/andSelf/
like image 107
Andy E Avatar answered Nov 07 '22 17:11

Andy E