JQuery AddClass()

jQuery addClass() method is the used to add one or more class name to the selected element.

Below the syntax of addClass().

$(selector).addClass(classname,function(index,currentclass));

Parameter Description
classname Required. Specifies one or more class names to be added
function(index,currentclass) Optional. Specifies a function that returns one or more class names to be added
  • index - Returns the index position of the element in the set
  • currentclass - Returns the current class name of the selected element
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery addClass()</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() {
     $("p").addClass('highlight');
  });
});
</script>
</head>
<body>
 <p>This is a paragraph.</p>
 <button style="margin:0 0 10px 0;background:#e8e8e8;" class="btn" id="button">Click here to add a class name to the p element</button><br />
 </body>
</html>