How to check a class exists on page or on html element.

satinder singh / Updated : / 0 Comments

 jQuery .hasClass() : This article explains how to Check whether an element has particular class. i.e how to find div contains certain class name exists.

In below sample code will show you how to get all div tags having class name public and then will change text of it.

In jquery this can be done with .hasClass( className ) method.


.hasClass( className ) // return boolean (true/false).

Description: Determine whether any of the matched elements is assigned the given class.

Here on button click will check whether a div tag contains specific class i.e here in our case we are checking for class name public as shown in below code.

   var publicDivs= $(“div”).hasClass(“public”);
   // gives div count having ‘public’ classname


Using .hasClass() jquery method will find out whether specfic element contains specific class.

Hope you enjoy this tutorial.

Post Comment

Your email address will not be published. Required fields are marked *