How to use hasclass() method in JQuery

In this article, I will explain use of hasclass() Method in JQuery.
  • 2289

JQuery CSS hasclass() Method

hasClass() method checks for selected element have a specified class. If ANY of the selected elements has the specified class, this method will return "true".

Syntax

$(selector).hasClass()

Example

The following example shows how to hasclass() method in jquery.

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

    $(document).ready(function () {

        $("button").click(function () {

            alert($("p").hasClass("HasClassIntro"));

        });

    });

</script>

<style type="text/css">

.HasClassIntro

{

font-style:italic;

font-size:120%;

color:red;

background-color:Purple;

border-left-style:dotted;

}

</style>

</head>

 

<body>

<h1>This is hasclass() method example</h1>

<p class="HasClassIntro">One paragraph.</p>

<p>Second paragraph.</p>

<button>Does any element have an "HasClassIntro" class?</button>

</body>

</html>

 

Output


hasclass method.jpg

 Further Readings

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2018 DotNetHeaven. All rights reserved.