• 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 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 Parent Div of an Element
  • 2.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 3.  jQuery delay – How to Delay the Start of a Method
  • 4.  Using jQuery to Hide a Div
  • 5.  jQuery next – Get the Next Element of a Div
  • 6.  How to Check if Element Exists Using jQuery
  • 7.  Using jQuery to Add CSS Important Style
  • 8.  Using jQuery to Delete an Element
  • 9.  jQuery hover – An Interactive Example of the hover Method
  • 10.  How to Set All Checkbox to Checked in jQuery

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