jQuery addClass()

Introduction

Add a class name to the first <p> element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/* w  w w  . j a v a  2 s.c o m*/
<script>
$(document).ready(function(){
  $("button").click(function(){
      $("p:first").addClass("intro");
  });
});
</script>
<style>
.intro {
  font-size: 150%;
  color: red;
}
</style>
</head>
<body>

<h1>This is a heading</h1>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

<button>Test</button>

</body>
</html>

The addClass() method adds one or more class names to the selected elements.

To add more than one class, separate the class names with spaces.

$(selector).addClass(classname,function(index,current_class))
Parameter
Optional
Description
classname
Required.
one or more class names to be added
function(index,current_class)


Optional.


sets a function that returns one or more class names to be added
index - the index position of the element
current_class - the current class name of the selected element



PreviousNext

Related