jQuery .siblings() get siblings of each element in matched elements

Syntax and Description

.siblings([selector])

gets the siblings of each element in the set of matched elements, optionally filtered by a selector. selector (optional) is a string containing a selector expression to match elements against. Its return value is the new jQuery object.

Consider a page with a simple list as follows:


<ul>/*  w w w  .  j  a  v  a2s .c o  m*/
 <li>list item 1</li>
 <li>list item 2</li>
 <li class="third-item">list item 3</li>
 <li>list item 4</li>
 <li>list item 5</li>
</ul>

$('li.third-item').siblings().css('background-color', 'red'); marks items 1, 2, 4, and 5.

As we do not supply a selector expression, all of the siblings are part of the object. If we had supplied one, only the matching items among these four would be included.





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities