We can use the jQuery hasClass method to check and see if any element has a specified class. The hasClass method will return true if the class is found, or false if it is not.
$('#div1').hasClass('sub-div');
Let’s say I have the following HTML:
This is some text.
This is some text.
This is some text.
This is some text.
This is some text.
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 p').hasClass('special-class')){
$('#div1').css('background','green');
}
As you can see, if a paragraph in 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 p').hasClass('special-class')){
jQuery('#div1').css('background','green');
}
Let’s take a look at a dynamic example of using the jQuery hasClass() method.
Using jQuery hasClass Method to Add Styles Based on the Content of a Div
In this example, we will use a random number generator to generate 3 numbers. For the HTML setup for this example, all we need is an empty div to start with, a results div, and a button that will run a function when clicked on.
Here is the simple HTML setup:
Click the button below to generate 3 random numbers and see if any are even.
Generate numbers and check
In the first part of the JavaScript code, we will generate a random number from 1 to 20.
Once we generate the numbers, we will check if each is even number or odd. We will then append the new number with a new span tag and a class of either even or odd, to our div, #numbers.
Finally, we will check if our #numbers div has any classes that are even, meaning that one of the 3 numbers is even. If it does, we will note that there is an even number in the list. If not, we mark that there are none. We will do this with the help of the text() method.
This example is just a simple way to show the power of the jQuery hasClass() method.
Here is the JavaScript and jQuery code:
$('#click-me').click(function(){
//First, clear div, random-numbers
$('#random-numbers').text('');
//Generate 3 random number between 1 and 20
for (var i=0; i < 3; i++){
var newNumber = Math.round(Math.random() * 20);
//Check if new number is even
if ((newNumber % 2) == 0) {
//Create a new span with class even and add it to the div
$('' + newNumber + '').appendTo("#random-numbers");
} else {
$('' + newNumber + '').appendTo("#random-numbers");
}
}
//Once we have added all the numbers, we can check if there are any even numbers
if ($('#random-numbers span').hasClass('even')){
$('#results').text('The list of numbers above HAS an even number in it.');
} else {
$('#results').text('The list of numbers above Does NOT Have an even number in it.');
}
});
The final code and output for this example is below:
Code Output:
Click the button below to generate 3 random numbers and see if any are even.
Full Code:
Click the button below to generate 3 random numbers and see if any are even.
Generate numbers and check
Hopefully this article has been useful for you to understand how to use the jQuery hasClass method.