Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: find siblings of the same element type

Tags:

jquery

Given an unorder nested list, if one finds a first occurrence of the "li", how does one find all the siblings that are on the same level?

root.find('li:first')...?

Update: My question was not formulated correctly. I actually need to know following. If I have a reference to element, how do I find elements siblings and create a set that will include element itself and the siblings of the same element type, in this case 'li'?

like image 923
epitka Avatar asked Dec 14 '22 00:12

epitka


1 Answers

If you want to include the original element as well, you'll need

$(this).siblings("li").andSelf();

Edit: jQuery has deprecated andSelf. This is still the top answer, so for future use addBack might be what you want

$(this).siblings("li").addBack();
like image 165
Blair Mitchelmore Avatar answered Dec 15 '22 13:12

Blair Mitchelmore