The siblings method
The siblings method finds all siblings of an element within a single parent.
Syntax
Getting neighbors:
.siblings();
You can also filter neighbors by a given selector (only those neighbors that satisfy this selector will remain in the set):
.siblings(selector);
Example
Let's find the element #test and set the text '!' to all its neighbors using the html method:
<p>outside</p>
<div>
<p>inside</p>
<p>inside</p>
<p id="test">inside</p>
<p>inside</p>
<p>inside</p>
</div>
<p>outside</p>
$('#test').siblings().html('!');
HTML the code will look like this:
<p>outside</p>
<div>
<p>!</p>
<p>!</p>
<p id="test">inside</p>
<p>!</p>
<p>!</p>
</div>
<p>outside</p>
Example
Let's find the element #test and all its neighbors with the class www we will set the text '!' using the method html:
<p>outside</p>
<div>
<p>inside</p>
<p class="www">inside</p>
<p>inside</p>
<p id="test">inside</p>
<p>inside</p>
<p class="www">inside</p>
<p>inside</p>
</div>
<p>outside</p>
$('#test').siblings('.www').html('!');
HTML the code will look like this:
<p>outside</p>
<div>
<p>inside</p>
<p class="www">!</p>
<p>inside</p>
<p id="test">inside</p>
<p>inside</p>
<p class="www">!</p>
<p>inside</p>
</div>
<p>outside</p>