Hello Readers,
hasClass() is the jquery method which is used to check the any classname of the selected elements. If we find the name of the class of the selected element it return true else return false.
Syntax :
$(selector).hasClass(classname)
Parameter :
classname : this is a required parameter which specifies the classname to search for in the selected elements.
Code Example :
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert($("p").hasClass("intro"));
});
});
</script>
<style>
.intro {
font-size: 100%;
color: yellow;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p class="intro">This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>button</button>
</body>
</html>
In the above code if we find any p element have an "intro" class than hasClass() return true else return false.In the above example return true because this method find the Classname "intro" inside the p element.
0 Comment(s)