Showing posts with label jQuery addClass() method. Show all posts
Showing posts with label jQuery addClass() method. Show all posts

jQuery addClass() method


jQuery addClass() method
The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute.

Syntax


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




Example :


<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p:first").addClass("intro");
    });
});
</script>
<style>
.intro {
    font-size: 310%;
    color: Pink;
}
</style>
</head>
<body>

<button> Add a class name to the first p element</button>
<h1>This is a heading....</h1>
<p>This is a paragraph....</p>
<p>This is another paragraph.....</p>
</body>
</html>