Open In App

jQuery toggleClass() Method

Last Updated : 10 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The toggleClass() method is an inbuilt method in jQuery that is used to toggle or change the class attached to the selected element.

Syntax:

$(selector).toggleClass(class, function, switch)

Parameters:

This method accepts three parameters as mentioned above and described below:

  • class: It is the required parameter and is used to specify the class name that needs to replace.
  • function: It is an optional parameter and is used to specify a function that returns one or more class names. This parameter contains the index position of the element and the class name of the element.
  • switch: It is an optional parameter and is used to specify either true or false. By default it is true.

Return Value: This method returns the selected element with the specified changes made by the toggleClass() method.

The below examples illustrate the toggleClass() method in jQuery:

Example 1:

html




<!DOCTYPE html>
<html>
 
<head>
    <title>The toggleclass Method</title>
    <script src=
    </script>
 
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("div").toggleClass("gfg");
            });
        });
    </script>
    <style>
        .gfg {
            font-size: 25px;
            background-color: yellow;
            min-height: 120px;
        }
 
        div {
            width: 200px;
            min-height: 120px;
            background-color: lightgreen;
            padding: 20px;
            font-weight: bold;
            font-size: 20px;
        }
    </style>
</head>
 
<body>
    <!-- click inside this div element to see the change -->
    <div>
        <p>Hello!</p>
        <p>Welcome to GeeksforGeeks.!</p>
        <button>Click Here!</button>
    </div>
</body>
 
</html>


Output:

jquery-39

Example 2:

html




<!DOCTYPE html>
<html>
 
<head>
    <title>toggle class property</title>
    <script src=
    </script>
 
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("div").toggleClass(function (n) {
                    n = 1;
                    return "item_" + n;
                });
            });
        });
    </script>
    <style>
        .item_1 {
            color: green;
            font-weight: bold;
            font-size: 20px;
            background-color: white;
            border: 2px solid green;
        }
 
        div {
            text-align: center;
            width: 200px;
            min-height: 100px;
            background-color: lightgreen;
            padding: 20px;
            border: 2px solid black;
            font-weight: bold;
        }
    </style>
</head>
 
<body>
    <!-- click inside this div element -->
    <div>
        <p>Welcome to GeeksforGeeks!</p>
        <button>Click Here!</button>
    </div>
</body>
 
</html>


Output:

jquery-40



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads