We can use the jQuery siblings method to get the all the siblings(divs) of the selected div.

$("#div1").siblings();

Let’s say we have the following HTML:

This is paragraph one.

This is a sibling to paragraph one.

This is paragraph two.

This is a sibling to paragraph two.

This is paragraph three.

This is a sibling to paragraph three.

If we want to change the text color of only the sibling paragraph of paragraph with class “p2”, we will use the jQuery siblings() method along with the css() method.

$(".p2").siblings().css("color", "green");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery(".p2").siblings().css("color", "green");

If we just wanted to get one sibling, we could use the jQuery next() method to get the next sibling only, or the jQuery prev() method to get the previous sibling only.

Using the jQuery siblings Method to Get and Change the Siblings of a Paragraph

In this example, we will get all of the siblings of the paragraph with class “p2” and change their text colors.

Here is the simple HTML setup.



This is the first starting paragraph.

This is a sibling.

This is a sibling.

This is the second starting paragraph.

This is a sibling.

This is a sibling.

This is the third starting paragraph.

This is a sibling.

This is a sibling.

This is the fourth starting paragraph.

This is a sibling.

This is a sibling.

Change text colors

We will utilize the jQuery click(), css(), and siblings() methods to change the text color of the paragraph with class “p2” and all of its siblings when the button is clicked. In our function, we will generate a random color and then apply that color as the new text color. Evertime the user clicks to change the text color, a new color should appear.

Here is the JavaScript code:

$("#click-me").click(function(){
  //Create a random color
  var randomColor = "#" + (Math.floor(Math.random()*16777215).toString(16));
  //First change the text color of the paragraph with class "p2"
  $(".p2").css("color", randomColor);
  //The change the text color of its siblings
  $(".p2").siblings().css("color", randomColor);
});

The final code and output for this example of using the jQuery siblings method is below:

Code Output:

This is the first starting paragraph.

This is a sibling.

This is a sibling.

This is the second starting paragraph.

This is a sibling.

This is a sibling.

This is the third starting paragraph.

This is a sibling.

This is a sibling.

This is the fourth starting paragraph.

This is a sibling.

This is a sibling.

Change text colors

Full Code:


This is the first starting paragraph.

This is a sibling.

This is a sibling.

This is the second starting paragraph.

This is a sibling.

This is a sibling.

This is the third starting paragraph.

This is a sibling.

This is a sibling.

This is the fourth starting paragraph.

This is a sibling.

This is a sibling.

Change text colors

Hopefully this article has been useful for you to understand how to use the jQuery siblings method.

Categorized in:

jQuery,

Last Update: February 26, 2024