Why doesn't the color change when adding a class?

  • 0
    I add a new class to the element, but the color does not change.
    How can I change the color by adding or removing this class?
    https://jsfiddle.net/5mqekaL2/
    JavaScript Anonymous, Sep 4, 2020

  • 1 Answers
  • 0
    ssList.add ("red");



    In this case, a class without a dot is written.



    document.querySelector(".box").addEventListener("click", function(e) {
    e.target.classList.toggle("red");
    })


    https://jsfiddle.net/nwrg8qhu/9/



    It is very easy to remember this. If you cannot specify anything besides a class, then you do not need to specify ".", Which indicated that this is a class.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!