0

i've been trying to understand toggleClass function by making this simple script, yet it didn't work the way I expected it to.

<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <style type="text/css">
        body {
            font-family: "Kozuka Gothic Pro";
        }

        .red {
            background: red;
        }

        .blue {
            background: blue;
        }

        div {
            width: 200px;
            height: 200px;
        }

    </style>
</head>

<body>
    <div class="blue"></div>

    <script>
        $("div").click(function() {
            $(this).toggleClass("red");
        });
    </script>
</body>

However, if i changed the div class to red and the toggleClass argument to "blue" it works, can anybody explain me this? I'm hoping to hear from you. Thanks in advance!

6 Answers 6

4

You need to add both blue and red class in toggleClass function to change both like,

$("div").click(function() {
     $(this).toggleClass("red blue");
});

$("div").click(function() {
  $(this).toggleClass("red blue");
});
body {
            font-family: "Kozuka Gothic Pro";
        }

        .red {
            background: red;
        }

        .blue {
            background: blue;
        }

        div {
            width: 200px;
            height: 200px;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="blue"></div>

What happens when you use only red in togleclass function then it will applied but the change would not overwrite because of the blue class and the blue background shown as it is. So, if you want your code to work then in that case you need to use !important in red-background like,

.red {
    background: red !important;
}

$("div").click(function() {
  $(this).toggleClass("red");
});
body {
            font-family: "Kozuka Gothic Pro";
        }

        .red {
            background: red !important;
        }

        .blue {
            background: blue;
        }

        div {
            width: 200px;
            height: 200px;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="blue"></div>

Note that I am just informing you (by second alternative) why your code was not working. You must go with my first option.

Sign up to request clarification or add additional context in comments.

3 Comments

Do not use important! That is BAD BAD advice.
Thanks for that, i see the reason why now!
@epascarello Agreed, but I have just gave an example how the OP's code can work without changing the js code and explained why his code was not working.
1

$("div").click(function() {
            
            $(this).toggleClass("red");
            $(this).toggleClass("blue");
        });
 body {
            font-family: "Kozuka Gothic Pro";
        }

        .red {
            background: red;
        }

        .blue {
            background: blue;
        }

        div {
            width: 200px;
            height: 200px;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <div class="blue"></div>

 $("div").click(function() {

        $(this).toggleClass("red");
        $(this).toggleClass("blue");
    });

Comments

0

Try this : pass both red and blue class, it will remove if present and add if not. So for first time it will add red but will remove blue.

$("div").click(function() {
        $(this).toggleClass("red blue");
    });

Comments

0

The toggleClass() method toggles between adding and removing one or more class names from the selected elements.

This method checks each element for the specified class names. The class names are added if missing, and removed if already set - This creates a toggle effect.

Explanation Below

  • it act as - if div have class "red" then REMOVE class "red" else ADD "red" class from div

Comments

0

The problem arises due to order of the css styles, as last css style will affect the output, so as css on class blue is declared last, so blue class is given precedence over red class.

So, to solve this, you should try to have only one class on the div, so that the order of the css should not matter.

$("div").click(function() {
    $(this).toggleClass("red blue");
});

Comments

0

The toggleClass() will remove the class if it is assigned to the element, or it will add it if it is not assigned to the element. Try this snippet as a demo:

$(document).ready(function() {
  $("div").click(function() {
    if ($(this).hasClass("red")) {
      alert("I am red! I'm turning red off.");
      $(this).toggleClass("red");
    } else if ($(this).hasClass("blue")) {
      alert("I am blue! I'm turning blue off.");
      $(this).toggleClass("blue");
    } else {
      alert("I am blank! Turning red on.");
      $(this).toggleClass("red");
    }
  });
});
    body {
      font-family: "Kozuka Gothic Pro";
    }
    .red {
      background: red;
    }
    .blue {
      background: blue;
    }
    div {
      width: 200px;
      height: 200px;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<div class="blue"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.