jQuery hasClass() method

The jQuery hasClass() method checks whether any of the matched elements are assigned the given class.

Source Code

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
   <style>
 	 .highlight { border : 2px solid #1E8CBE; }
  </style>
</head>
<body>
  
<div class="div1">Div 1</div>
<div>Div 2</div>

<button>Click Here</button>

  
<script>
jQuery("button").click(function () {
	if(jQuery("div").hasClass("div1")) {
		jQuery(".div1").addClass("highlight");
	}
});
</script>
</body>
</html>

Demo

Following example find the div that contains ‘div1’ class.

Div 1
Div 2

,

No comments yet.

Leave a Reply