• 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 / Using jQuery to Get the Next Sibling of an Element

Using jQuery to Get the Next Sibling of an Element

January 30, 2022 Leave a Comment

To get the next sibling of an element we can use the jQuery next() method.

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

Let’s say we have the following HTML:

<div id="top-div">
  <div id="div1" class="divs">
    <p>This is paragraph one.</p>
  </div>
  <div class="divs">
    <p>This is paragraph two.</p>
  </div>
  <div class="divs">
    <p>This is paragraph three.</p>
  </div>
</div>

If we want to change the background color of only the div that contains paragraph two, we will use the jQuery next() method along with the css() method.

$("#div1").next().css("background", "green");

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

jQuery("#div1").next().css("background", "green");

Using jQuery to Get and Change the Next Sibling of an Element

In this example, we will get the sibling div of the first div and change its background color. We will also add some simple styles to the divs.

Here is the simple HTML setup.

<style>.divs { padding: 20px; border: 1px solid #000; margin-bottom: 10px; }</style>
<div id="top-div">
  <div id="div1" class="divs">
    <p>This is paragraph one.</p>
  </div>
  <div class="divs">
    <p>This is paragraph two.</p>
  </div>
  <div class="divs">
    <p>This is paragraph three.</p>
  </div>
</div>
<div id="click-me">Change background</div>

We will utilize the jQuery click(), css(), and next() methods to change the background color of the second div when the button is clicked.

Here is the JavaScript code:

$("#click-me").click(function(){
    $("#div1").next().css("background", "#c1e9c1");
});

The final code and output for this example of how to get and change the next sibling of an element using jQuery is below:

Code Output:

This is paragraph one.

This is paragraph two.

This is paragraph three.

Change background

Full Code:

.divs { padding: 20px; border: 1px solid #000; margin-bottom: 10px; }</style>
<div id="top-div">
  <div id="div1" class="divs">
    <p>This is paragraph one.</p>
  </div>
  <div class="divs">
    <p>This is paragraph two.</p>
  </div>
  <div class="divs">
    <p>This is paragraph three.</p>
  </div>
</div>
<div id="click-me">Change background</div>

<script>

$("#click-me").click(function(){
    $("#div1").next().css("background", "#c1e9c1");
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery to get the next sibling of an element.

Other Articles You'll Also Like:

  • 1.  jQuery width() – Using jQuery to Get the Width of a Div
  • 2.  jQuery mouseenter – An Interactive Example of the mouseenter Method
  • 3.  jQuery first() – Get the First Element
  • 4.  Using jQuery to Capitalize the First Letter of a String
  • 5.  Using jQuery to Delete an Element
  • 6.  Using jQuery to Show a Div
  • 7.  Using jQuery to see if URL Contains Specific Text
  • 8.  Using jQuery to Get Text Length
  • 9.  Using jQuery to Toggle Class of HTML Element
  • 10.  Using jQuery to get Textarea Value

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