hasClass() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:hasClass

Description

The hasClass() method checks if selected elements have a specified class name.

Syntax

Parameter Require Description
classname Required.class name to search for in the selected elements

The following code shows how to Check if any <p> element has a class named "intro":

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.intro {//from   www  . j  ava  2s  . c  o m
    font-size: 120%;
    color: red;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        console.log($("p").hasClass("intro"));
    });
});
</script>
</head>
<body>

<h1>This is a heading</h1>

<p class="intro">This is a paragraph.</p>
<p>This is another paragraph.</p>

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

</body>
</html>

Related Tutorials