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

 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.

Syntax:

.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.

//*
$(“#btnGetAllPublicDiv”).on(‘click’,function(){
   var publicDivs= $(“div”).hasClass(“public”);
   // gives div count having ‘public’ classname
    console.log(publicDivs.length);  
});
//*

Conclusion:

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

Hope you enjoy this tutorial.

Share:    
Satinder Singh

Hi all, am Founder and Author of Codepedia.info, also atypical polyglot programmer from Bombay, India. I have a passion to create, solve, and deploy software applications.
       ForEach ( minute in MyLife ) { myExperience++ ; }



Post Comment


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