jQuery hasClass()
method is the used to determine whether any of the matched elements are assigned the given class.
Below the syntax of hasClass()
.
$(selector).hasClass(className);
Parameter |
Description |
className |
The class name to search for. |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery hasClass()</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
.highlight{font-size:18px;color:#11a286}
</style>
<script>
$(document).ready(function() {
$("#button").click(function() {
alert($("p").hasClass('highlight'));
});
});
</script>
</head>
<body>
<p class="highlight">This is a paragraph.</p>
<button style="margin:0 0 10px 0;background:#e8e8e8;" class="btn" id="button">Click here to check if p element have an "highlight" class?</button><br />
</body>
</html>