jQuery hasClass( class ) Method

« Previous Chapter Next Chapter »


The hasClass( class ) method returns true if the specified class is present on at least one of the set of matched elements otherwise it returns false.


Here is the simple syntax to use this method:


selector.hasClass( class )


Here is the description of all the parameters used by this method:

  • class: The name of CSS class.


Following example would check which para has class red:


<title>The Selecter Example</title>
<script type="text/javascript" src="../../js/jquery-2.2.0.js">
<script type="text/javascript" language="javascript">
   $(document).ready(function() {
     alert("First para returns: " +
               $("p#pid1").hasClass("red") );
     alert("Second para returns: " + 
               $("p#pid2").hasClass("red") );
  .red { color:red; }
  .green { color:green; }
<p class="red" id="pid1">This is first paragraph.</p>
<p class="green" id="pid2">This is second paragraph.</p>

This would generate following Output Will Be :


First para returns: true
Second para returns: false
Code It Online

« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!