• 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 has – Check if an Element Has Other Elements

jQuery has – Check if an Element Has Other Elements

March 27, 2022 Leave a Comment

We can use the jQuery has method to check and retrieve any elements that have another element or specified class.

$('#div1').has('sub-div');

Let’s say I have the following HTML:

<div id="div1">
  <p>This is some text.</p>
  <p class="special-class">This is some text.</p>
  <p>This is some text.</p>
  <p>This is some text.</p>
  <p>This is some text.</p>
</div>

If we want to check the div, #div1, to see if any of the paragraphs in it have a class called “special-class”, we can do the following code:

if ($('#div1').has('special-class')){
  $('#div1').css('background','green');
}

As you can see, if the div does contain that class, we will change the background color of the div to green.

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

if (jQuery('#div1').has('special-class')){
  jQuery('#div1').css('background','green');
}

Using the jQuery has Method to Add Styles Based on the Content of a Div

In this example, we will check if a div has a paragraph with a specific class. If it does, we will change the background color of the div.

Here is the simple HTML setup:

<div id="div1">
  <p>This is some text.</p>
  <p class="special-class">This is some text.</p>
  <p>This is some text.</p>
  <p>This is some text.</p>
  <p>This is some text.</p>
</div>
<div id="click-me">Check class</div>

In the JavaScript part of this example, we first run a function when the user clicks the check button.

In our function, we simply check if the class, special-class, exists in one of the paragraphs in the div, #div1.

To do this we will use the jQuery has() method.

Since the jQuery has() method returns each element that has the specified class, which will be #div1 in this case, we can then change the background color of it right after the has() method call.

Here is the JavaScript and jQuery code:

$('#click-me').click(function(){
  $("#div1").has(".special-class").css("background", "#7bbfa2");
});

The final code and output for this example is below:

Code Output:

This is some text.

This is some text.

This is some text.

This is some text.

This is some text.

Check class

Full Code:

<div id="div1">
  <p>This is some text.</p>
  <p class="special-class">This is some text.</p>
  <p>This is some text.</p>
  <p>This is some text.</p>
  <p>This is some text.</p>
</div>
<div id="click-me">Check class</div>

<script>

$('#click-me').click(function(){
  $("#div1").has(".special-class").css("background", "#7bbfa2");
});

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Get the Top Position of Element
  • 2.  Using jQuery to Check if Checkbox is Checked
  • 3.  jQuery mouseup – An Interactive Example of the jQuery mouseup Method
  • 4.  Using jQuery to Set Visibility
  • 5.  jQuery get first child
  • 6.  jQuery get img src – Get the Source of an Image Using jQuery
  • 7.  Toggle the Visibility of an Element Using jQuery
  • 8.  How to Use jQuery to Change Button Text
  • 9.  Get Padding of an Element Using jQuery
  • 10.  Using jQuery to Change Label Text

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