• 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
  • VBA
  • About
You are here: Home / jQuery / jQuery next – Get the Next Element of a Div

jQuery next – Get the Next Element of a Div

March 22, 2022 Leave a Comment

We can use the jQuery next method to get the next sibling(div) of the selected div.

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

The jQuery next() method is very similar to the jQuery prev method, it just gets a different sibling.

Using the jQuery next method to Get and Change the Next Sibling of a Div

In this example, we will get the sibling div of the first div and change its background color. The divs we have will all be boxes. We will add some initial styles to them.

Here is the simple HTML setup.


<style>.divs { display: inline-block; width: 50px; height: 50px; background: #ddd; margin-bottom: 10px; margin-right: 10px; }</style>
<div id="top-div">
  <div id="div1" class="divs"></div>
  <div class="divs"></div>
  <div class="divs"></div>
</div>
<div id="click-me">Change background of second box</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. In our function, we will generate a random color and then apply that color as the background to the second div. Evertime the user clicks to change the background, 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));
  $("#div1").next().css("background", randomColor);
});

The final code and output for this example of using the jQuery next method to get and change the next sibling of a div:

Code Output:

Change background of second box

Full Code:


<style>.divs { display: inline-block; width: 50px; height: 50px; background: #ddd; margin-bottom: 10px; margin-right: 10px; }</style>
<div id="top-div">
  <div id="div1" class="divs"></div>
  <div class="divs"></div>
  <div class="divs"></div>
</div>
<div id="click-me">Change background of second box</div>

<script>

$("#click-me").click(function(){
  //Create a random color
  var randomColor = "#" + (Math.floor(Math.random()*16777215).toString(16));
  $("#div1").next().css("background", randomColor);
});

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Convert a String to Lowercase
  • 2.  Using jQuery to Remove Background Color
  • 3.  jQuery width() – Using jQuery to Get the Width of a Div
  • 4.  jQuery mouseleave – An Interactive Example of the mouseleave Method
  • 5.  Using jQuery prepend to Insert Element As First Child
  • 6.  jQuery keydown Method
  • 7.  How to Clear an Input Field Using jQuery
  • 8.  jQuery insertBefore – Insert Element Before Another Element
  • 9.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 10.  Using jQuery to Capitalize the First Letter of a String

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy