Introduction:

In this article we will explain what is toggleCLass() method in jQuery or how to once click add class name from html selected elements or how to remove class name once clicking from selected elements in jQuery with example.

Description:

The jQuery toggleCLass() method is used to add or remove one or more classes from the html selected elements. This method toggles between adding and removing one or more class name. It checks each element for the specified class names. If the class name is already set, it removes and if the class name is not exists, it adds

Let’s see, Example of jQuery toggleCLass() method:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Example of jQuery toggleClass method</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("#maindiv").toggleClass("Myclass");
            });
        });
    </script>
    <div class="div" id="maindiv"></div>    
    <button>Click here to add/remove class</button>
    <style>
        .div {
            height: 100px;
            width: 200px;
            background: #77c4fe;
            margin-bottom: 10px;
        }
        .Myclass {
            background: #f66262;
            height: 150px;
            width: 300px;
        }
    </style>
</body>
</html>


 Let's check live example

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like