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


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.

Example of jQuery toggleClass() method
<!DOCTYPE html>
<html xmlns="">
    <title>Jquery Tutorials</title>
    <script src="Scripts/jquery-1.8.2.min.js"></script>
        $(document).ready(function () {
            $("button").click(function () {
        <div class="div"></div>
        <div class="div"></div>
    <button>Click here to add/remove class</button>
        .div {
            height: 100px;
            width: 200px;
            background: #77c4fe;
            margin-bottom: 10px;            
        .Myclass {
            background: #f66262;


Use jQuery toggleClass()

When we click once on button “Myclass” class name adds in div and click once again “Myclass” class name removed fron div. like shown as below image:

Use jQuery toggleClass()

click here to read more Jquery tutorials by Aditya Kumar Patel

  Modified On Sep-07-2019 12:18:02 AM

Leave Comment