jQuery addClass()

The jQuery addClass() method adds one or more class name to the selected element.

Syntax:

$(selector).addClass(classname,function(index,oldclass)) 

Classname:

  • Classname is a compulsory parameter of jQuery addClass() method, as it specifies the class names to be added.

Function:

  • It is an optional parameter.
  • The function parameter is used to return the property‚Äôs class names to be added.

Index:

  • Index is an argument passed within the function.
  • It is used to give an index position to an element in the set.

Currentclass:

  • This parameter returns the current class name of the selected element.

Example 1

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>    
<script>  
$(document).ready(function(){  
	$("button").click(function(){  
    	$("p:first").addClass("yo");  
	});  
});  
</script>  
<style>  
.yo {  
	font-size: 250%;  
	color: red;  
}  
</style>  
</head>  
<body>  
<h1>Hello!</h1>  
<p>How are you?</p>  
<p>Nice to see you again.</p>  
<button>Click me</button>  
</body>  
</html>

Try it

JS Bin on jsbin.com

Please Share