• 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 / 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.  Using jQuery to Change Text of HTML Element
  • 2.  Using jQuery to Replace a Class
  • 3.  jQuery fadeIn – Fading In Element in HTML
  • 4.  Using jQuery to Change the Id of a Div
  • 5.  onclick this jQuery
  • 6.  How to Set All Checkbox to Checked in jQuery
  • 7.  jQuery insertAfter – Insert Element After Another Element
  • 8.  jQuery focus – Changing Form Styles with the focus() Method
  • 9.  jQuery mouseover – An Interactive Example of the mouseover Method
  • 10.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript

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