Copyright © tutorialspoint.com

jQuery - hasClass( class ) Method

previous next


Advertisements

Description:

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.

Syntax:

Here is the simple syntax to use this method:

selector.hasClass( class )

Parameters:

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

Example:

Following example would check which para has class red:

<html>
<head>
<title>The Selecter Example</title>
<script type="text/javascript" src="/jquery/jquery-1.3.2.min.js">
</script>
   
<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") );

   });

</script>
<style>
  .red { color:red; }
  .green { color:green; }
</style>

</head>
<body>

<p class="red" id="pid1">This is first paragraph.</p>
<p class="green" id="pid2">This is second paragraph.</p>

</body>
</html>

This would generate following result:

First para returns: true
Second para returns: false

To understand it in better way you can Try it yourself.


previous next

Copyright © tutorialspoint.com