jQuery hasClass() with Examples

The hasClass() method in jQuery is used to check if any of the selected elements is having a specified class name.


The syntax is as follows −


Above, the parameter class name is the class name to search for in the selected elements.


Let us now see an example to implement the jQuery hasClass() method

 Live Demo

<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<h2>Student Info</h2>
<h2 class="demo">Teacher Info</h2>
<button>Click me</button>


This will produce the following output−


Click the “Click me” button to display whether any <h2> element has class “demo”: