jQuery hasClass() Method

jQuery hasClass() method used to checks given class exist of the selected elements.

The hasClass() method will return true if the class is exist on any of the selected elements.

Syntax

Here is a syntax for hasClass() method

$(selector).hasClass( className );
Parameter Type Description
className String Required. Specifies the class name.

Example

<!DOCTYPE html>
<html>
<head>
  <title>jQuery hasClass() method</title>
  <script src="jquery-latest.min.js"></script>
  <script>
    $(document).ready(function(){
      $("button").click(function(){
        alert($("p").hasClass("param"));
      });
    });
  </script>
  <style type="text/css">
    .param {
      font: 16px Verdana, Arial;
      font-weight:bold;
      color:pink;
    }
  </style>
</head>
<body>
  <p>First paragraph start here...</p>
  <p class="param">Second paragraph start here...</p>
  <p>Third paragraph start here...</p>
  <button>Click here to call hasClass() method</button>
</body> 
</html>

Run it...   »