From jQuery JavaScript Library
« Back to Attributes
hasClass( class )
Returns true if the specified class is present on at least one of the set of matched elements.
Arguments:| class | String | |
|---|
| One CSS class name to be checked for. |
Examples:| Name | Type |
Looks for the class 'selected' on the matched elements.
$("div#result1").append($("p:first").hasClass("selected").toString());
$("div#result2").append($("p:last").hasClass("selected").toString());
$("div#result3").append($("p").hasClass("selected").toString());
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("div#result1").append($("p:first").hasClass("selected").toString());
$("div#result2").append($("p:last").hasClass("selected").toString());
$("div#result3").append($("p").hasClass("selected").toString());
});
</script>
<style>
p { margin: 8px; font-size:16px; }
.selected { color:red; }
.highlight { background:yellow; }
</style>
</head>
<body>
<p>Hello</p>
<p class="selected">Goodbye</p>
<div id="result1">First paragraph has selected class: </div>
<div id="result2">Last paragraph has selected class: </div>
<div id="result3">Some paragraph has selected class: </div>
</body>
</html>