jQuery addClass()

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

Below the syntax of addClass().


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">
<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>
$(document).ready(function() {
  $("#button").click(function() {
 <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 />