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

/ / 0 Comments

jQuery check is classname exists : Here in this article will explain 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.

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to do a favor, then Buy me a coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.


Post Comment

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

0 Comments