• 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 / Using jQuery to Check if Element Has Class

Using jQuery to Check if Element Has Class

December 12, 2021 Leave a Comment

To check if an element has class using jQuery, the simplest way is to use the hasClass() method.

$('#element').hasClass('example-class');

Let’s say I have the following HTML:

<div id="div">
  <div id="div-to-check" class="check-me">This is a div we will check.</div>
</div>

If we want to check the div to see if it has the class “check-me”, we can do the following:

var hasClassCheckMe = $('#div-to-check').hasClass('check-me');
console.log(hasClassCheckMe); // true

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

var hasClassCheckMe = jQuery('#div-to-check').hasClass('check-me');
console.log(hasClassCheckMe); // true

Checking if Element Has Class Using jQuery On Click

We can check if a HTML element has a class on click using jQuery very easily by combining the hasClass() method with a click event.

Let’s say we have the following HTML code and we want to display if our div has the class “check-me” and “check-me-too” in the span below:

<div id="div1">
  <div id="div-to-check" class="check-me">We will check this div for class "check-me"</div>
  <p>Does the div "div-to-check" have class "check-me"? <b><span id="hasClass-value-1"></span></b>.</p>
  <p>Does the div "div-to-check" have class "check-me-too"? <b><span id="hasClass-value-2"></span></b>.</p>
  <div id="click-me">Check</div>
</div>

We can utilize both the jQuery click() method and jQuery hasClass() method to find if the div has class “check-me”. Then we will change the text of the span and display true or false.

Below is the JavaScript code which will allow us to see if our element has the class “check-me” and “check-me-too” then display the two values.

$('#click-me').click(function(){
  $("#hasClass-value-1").text($('#div-to-check').hasClass('check-me'));
  $("#hasClass-value-2").text($('#div-to-check').hasClass('check-me-too'));
});

The final code and output for this example of how to check if an element has a class on a click using jQuery and JavaScript is below:

Code Output:

We will check this div for class “check-me”

Does the div “div-to-check” have class “check-me”? .

Does the div “div-to-check” have class “check-me-too”? .

Check

Full Code:

<div id="div1">
  <div id="div-to-check" class="check-me">We will check this div for class "check-me"</div>
  <p>Does the div "div-to-check" have class "check-me"? <b><span id="hasClass-value-1"></span></b>.</p>
  <p>Does the div "div-to-check" have class "check-me-too"? <b><span id="hasClass-value-2"></span></b>.</p>
  <div id="click-me">Check</div>
</div>

<script>

$('#click-me').click(function(){
  $("#hasClass-value-1").text($('#div-to-check').hasClass('check-me'));
  $("#hasClass-value-2").text($('#div-to-check').hasClass('check-me-too'));
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery to check if an element has a certain class.

Other Articles You'll Also Like:

  • 1.  How to Use jQuery to Animate the Font Size of a Paragraph
  • 2.  jQuery rotate – How to Rotate an Image using jQuery
  • 3.  jQuery hasClass – How to Check if an Element Has a Certain Class
  • 4.  How to Clear an Input Field Using jQuery
  • 5.  Using jQuery to Add Required Attribute to Input Field
  • 6.  Using jQuery to Hide a Div
  • 7.  Using jQuery to Select Multiple Classes
  • 8.  Using jQuery to Change Label Text
  • 9.  Using jQuery to Add id to div
  • 10.  jQuery has – Check if an Element Has Other Elements

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