hasClass()

The jQuery hasClass() method is used to check whether selected elements have specified class name or not. It returns TRUE if the specified class is present in any of the selected elements otherwise it returns FALSE.

Syntax:

snippet
$(selector).hasClass(classname)

Parameters of jQuery hasClass() method

Parameter Description
className It is a mandatory parameter. It specifies the name of the CSS class to search for in the selected elements.

Example of jQuery hasClass() method

Let's take an example to demonstrate jQuery hasClass() method.

snippet
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        alert($("p").hasClass("intro"));
    });
});
</script>
<style>
.intro {
    font-size: 150%;
    color: Blue;
}
</style>
</head>
<body>
<h1>Look here, I am a heading.</h1>
<p class="intro">This is a paragraph.</p>
<p>This is also a paragraph.</p>
<button>Click here to check if any p element have an "intro" class?</button>
</body>
</html>

jQuery hasClass() method example 2

Let's take another example to demonstrate jQuery hasClass() method.

snippet
<!DOCTYPE html>
<html>
<head>
<title>The Selecter Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#result1").text( $("p#pid1").hasClass("red") );
$("#result2").text( $("p#pid2").hasClass("red") );
});
</script>
<style>
.red { color:red; }
.blue { color:blue; }
</style>
</head>
<body>
<p class="red" id="pid1">This is first paragraph.</p>
<p class="blue" id="pid2">This is second paragraph.</p>
<div id="result1"></div>
<div id="result2"></div>
</body>
</html>
Note
Here the first condition is true and the second is false. If we set the second paragraph class name ?blue? then both the conditions will be true.

jQuery hasClass() Example 3

snippet
<!DOCTYPE html>
<html>
<head>
<title>The Selecter Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#result1").text( $("p#pid1").hasClass("red") );
$("#result2").text( $("p#pid2").hasClass("blue") );
});
</script>
<style>
.red { color:red; }
.blue { color:blue; }
</style>
</head>
<body>
<p class="red" id="pid1">This is first paragraph.</p>
<p class="blue" id="pid2">This is second paragraph.</p>
<div id="result1"></div>
<div id="result2"></div>
</body>
</html>

Here both the conditions will be true.

jQuery hasClass() Example 4

snippet
<!DOCTYPE html>
<html>
 <head> 
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script>
  $(document).ready(function() {   
   $(".btn").click(function(){ 
    var className = $(this).attr("id");
    $("ul li").each(function() {     
     if ($(this).hasClass(className)) {
      $(this).fadeTo('slow', 0.0).fadeTo('slow', 1.0);      
     }
    });    
   }); 
  });
  </script>   
  <style>
  ul{
   font-family: monospace;
   font-size: 15px;
   font-family: monospace; 
   font-style: normal;
   font-size-adjust: none;
   width:200px;   
   padding:0px; 
  }
   
  ul li{
   background-color:#7fffd4;
   width:100px;
   margin:5px;
   padding:5px;
   list-style-type:none;
   width:200px;
  }
  </style>
 </head>
 <body>
 <h1>jQuery .hasClass() function Example</h1>   
 <ul>
  <li class="red">Red</li>  
  <li class="green">Green</li>     
  <li class="green red">Green Red</li>
  <li class="blue">Blue</li>
 </ul> 
 <input type="button" class="btn" value="Red Class" id="red">
 <input type="button" class="btn" value="Green Class" id="green">
 <input type="button" class="btn" value="Blue Class" id="blue">
 <input type="button" class="btn" value="No Matching Class" id="noclass"> 
 </body>
</html>
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +