Open In App
Related Articles

How to add a new class to an element that already has a class using jQuery ?

Improve Article
Improve
Save Article
Save
Like Article
Like

In this article, we will learn to add a new class to an element that already has a class using jQuery. To add a new class, we use jQuery addClass() method. The addClass() method is used to add more classes and their attributes to each selected element. It can also be used to change the property of the selected element.

Syntax:

$(selector).addClass(className);

Parameters: It accepts a parameter “className”, the name of the new class that is to be added.

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <style>
        .GFG1 {
            color: green;
            font-weight: bold;
        }
  
        .GFG2 {
            font-size: 24px;
        }
    </style>
  
    <!-- Import jQuery cdn library -->
    <script src=
    </script>
  
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("p").addClass("GFG2");
            });
        });
    </script>
</head>
  
<body style="text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <h3>
        How to add a new class to an element that
        <br>already has a class using jQuery?
    </h3>
  
    <p class="GFG1">
        GeeksforGeeks computer science portal
    </p>
  
    <button>Click Here!</button>
</body>
  
</html>


Output:

Before Click the Button:

After Click Button:


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 31 Dec, 2020
Like Article
Save Article
Similar Reads
Related Tutorials