• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / jQuery siblings – Get the Sibling Elements of a Div

jQuery siblings – Get the Sibling Elements of a Div

March 22, 2022 Leave a Comment

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:

<div id="top-div">
  <div class="divs">
    <p class="p1">This is paragraph one.</p>
    <p>This is a sibling to paragraph one.</p>
  </div>
  <div class="divs">
    <p class="p2">This is paragraph two.</p>
    <p>This is a sibling to paragraph two.</p>
  </div>
  <div class="divs">
    <p class="p3">This is paragraph three.</p>
    <p>This is a sibling to paragraph three.</p>
  </div>
</div>

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.


<style></style>
<div id="top-div">
  <div class="divs">
    <p class="p1">This is the first starting paragraph.</p>
    <p>This is a sibling.</p>
    <p>This is a sibling.</p>
  </div>
  <div class="divs">
    <p class="p2">This is the second starting paragraph.</p>
    <p>This is a sibling.</p>
    <p>This is a sibling.</p>
  </div>
  <div class="divs">
    <p class="p3">This is the third starting paragraph.</p>
    <p>This is a sibling.</p>
    <p>This is a sibling.</p>
  </div>
  <div class="divs">
    <p class="p4">This is the fourth starting paragraph.</p>
    <p>This is a sibling.</p>
    <p>This is a sibling.</p>
  </div>
</div>
<div id="click-me">Change text colors</div>

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:

<style></style>
<div id="top-div">
  <div class="divs">
    <p class="p1">This is the first starting paragraph.</p>
    <p>This is a sibling.</p>
    <p>This is a sibling.</p>
  </div>
  <div class="divs">
    <p class="p2">This is the second starting paragraph.</p>
    <p>This is a sibling.</p>
    <p>This is a sibling.</p>
  </div>
  <div class="divs">
    <p class="p3">This is the third starting paragraph.</p>
    <p>This is a sibling.</p>
    <p>This is a sibling.</p>
  </div>
  <div class="divs">
    <p class="p4">This is the fourth starting paragraph.</p>
    <p>This is a sibling.</p>
    <p>This is a sibling.</p>
  </div>
</div>
<div id="click-me">Change text colors</div>

<script>

$("#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);
});

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Append Text to textarea Field
  • 2.  How to Use jQuery to Check if an Element is Visible
  • 3.  Using jQuery to Set a Radio Button to Checked
  • 4.  Using jQuery to Delete an Element
  • 5.  Using jQuery to Increment Value on Click
  • 6.  Using jQuery to Add a Sibling to an Element
  • 7.  Using jQuery to Get the Margin of an Element
  • 8.  How to Clear an Input Field Using jQuery
  • 9.  jQuery prependTo – Insert Element As First Child
  • 10.  Using jQuery to Change Inner HTML

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy