• 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 parent – Get the Parent Element of a Div

jQuery parent – Get the Parent Element of a Div

March 22, 2022 Leave a Comment

We can use the jQuery parent method to get the parent element(div) of the selected div.

$("#p1").parent();

Let’s say we have the following HTML:

<div id="top-div">
  <div>
    <p>This is paragraph one.</p>
  </div>
  <div>
    <p class="p2">This is paragraph two.</p>
  </div>
  <div>
    <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 can use the jQuery parent() method along with the css() method.

$(".p2").parent().css("background", "green");

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

jQuery(".p2").parent().css("background", "green");

Using the jQuery parent Method to Get and Change the Parent Div

In this example, we will get the parent of any div with a paragraph in it and change its background color. We will also add some simple styles to the divs.

Here is the simple HTML setup.


<style>.divs { float: left; width: 120px; height: 120px; padding: 10px; background: #ddd; margin-bottom: 10px; margin-right: 10px; }</style>
<div id="top-div">
  <div class="divs"></div>
  <div class="divs">
    <p>This is some text.</p>
  </div>
  <div class="divs"></div>
  <div class="divs"></div>
  <div class="divs">
    <p>This is some text.</p>
  </div>
  <div class="divs"></div>
  <div class="divs"></div>
  <div class="divs">
    <p>This is some text.</p>
  </div>
  <div class="divs"></div>
  <div class="divs"></div>
</div>
<div class="clear"></div>
<div id="click-me">Change background</div>

We will utilize the jQuery click(), css(), and parent() methods to change the background color of any div with a paragraph in it when the button is clicked. In our function, we will generate a random color and then apply that color as the background to the 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));
  $("#top-div p").parent().css("background", randomColor);
});

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

Code Output:

This is some text.

This is some text.

This is some text.

Change background

Full Code:


<style>.divs { float: left; width: 120px; height: 120px; padding: 10px; background: #ddd; margin-bottom: 10px; margin-right: 10px; }</style>
<div id="top-div">
  <div class="divs"></div>
  <div class="divs">
    <p>This is some text.</p>
  </div>
  <div class="divs"></div>
  <div class="divs"></div>
  <div class="divs">
    <p>This is some text.</p>
  </div>
  <div class="divs"></div>
  <div class="divs"></div>
  <div class="divs">
    <p>This is some text.</p>
  </div>
  <div class="divs"></div>
  <div class="divs"></div>
</div>
<div class="clear"></div>
<div id="click-me">Change background</div>

<script>

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

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Get the Position of Element
  • 2.  jQuery val Method – Get the Value from an Input Field
  • 3.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 4.  Using jQuery to Get the Value of a Radio Button
  • 5.  Using jQuery to Set Select to the First Option
  • 6.  jQuery siblings – Get the Sibling Elements of a Div
  • 7.  How to Clear an Input Field Using jQuery
  • 8.  jQuery keydown Method
  • 9.  Using jQuery to Scroll to Div
  • 10.  jQuery get first child

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