jQuery .toggleClass() does not toggle off the class after its toggled on

jquery toggleclass not working
jquery toggle class onclick
jquery toggle between two classes
toggle class javascript vanilla
jquery replace class
jquery toggle multiple classes
jquery add remove class onclick
jquery has class

In order to change the color scheme of the site, I've added an alternate class that was suppose to toggle on and off through a button with the usage of .toggleClass(), however, when I do toggle the class on, it doesn't seem to toggle back off when I click on the button again.

CSS

/*
Alternate color (dark) scheme
*/
 body.dark {
    background-color:#1e1d1b;
}
body.dark #main {
    background:#292826 url(background_dark_slice.jpg) repeat-x;
}
body.dark #main .container {
    background-image:url(background_dark.jpg);
}
body.dark #footer {
    background-image:url(background_dark_footer.jpg);
}
body.dark ul#menu li a.active, ul#menu li a:hover {
    color:#ffffff;
}

jQuery

$(document).ready(function(){
    var button = document.querySelector('.forjs button');
    button.addEventListener('click', function(){
        $('.dark').toggleClass();
    });
  });

  1. In the toggleClass() function you must be provided with a string representing the class to be checked for, and to be added/removed if present/absent: toggleClass('class');

  2. If you were to do something like $('.myClass').toggleClass('myClass') - it would only work the first time you fire it - as the second time, there would be no more elements with .myClass in the document - you just removed it in the previous function call.

  3. As you have not provided the HTML also, we cannot know for sure that the document.querySelector('.forjs button') is accurate - please test if this is so, or provide the adequate HTML code.

  4. In your CSS, you have defined the changes to take place when the body has a class of .dark : body.dark {}. So, in your case, the toggleClass() should be applied to the $(body) : $('body').toggleClass('dark');

You end function should be :

$(document).ready(function(){
    var button = document.querySelector('.forjs button');
    button.addEventListener('click', function(){
        $('body').toggleClass('dark');
    });
});

As you are mixing JQuery and vanilla JS, it would be advisable to keep consistent with one or the other.

Vanilla JS:

document.querySelector('.forjs button').addEventListener('click', function(){
   document.body.classList.toggle('dark');
});

Jquery :

$('.forjs button').on('click', function(){
    $('body').toggleClass('dark');
});

.toggleClass(), Description: Add or remove one or more classes from each element in the set of matched elements, depending on either A boolean (not just truthy/falsy) value to determine whether the class should be added or removed. toggleClass() is called will be toggled. Toggle the class 'highlight' when a paragraph is clicked. Hey mike. Thanks for the advice. Concatenating the two classes did work for the find part of the toggleClass, but the reassignment failed - the reason being; when it drops the new class name in there, it actually looks like this:

Try this instead: $('body').toggleClass('dark');

ToggleClass() is not toggling classes [SOLVED], ToggleClass() is not toggling classes [SOLVED] Here is the code I tried to write, but it doesn't take the "active" off the tab_01 anchor: the reassignment failed - the reason being; when it drops the new class name in there,� As of jQuery 1.4, if no arguments are passed to.toggleClass (), all classes on the element the first time.toggleClass () is called will be toggled. Also as of jQuery 1.4, the class name to be toggled can be determined by passing in a function. 1 2

  $('.dark').toggleClass('class_name');

http://api.jquery.com/toggleclass/ toggleClass have parameter more than zero

jQuery toggleClass() Method, Example. Toggle between adding and removing the "main" class name for all <p > elements: The toggleClass() method toggles between adding and removing one or more class names from the selected elements. However, by using the " switch" parameter, you can specify to only remove, or only add a class name. Learn all about the jQuery function.toggleClass (). This method takes one or more class names as its parameter. In the first version, if an element in the matched set of elements already has the class, then it is removed; if an element does not have the class, then it is added. For example, we can apply.toggleClass () to a simple <div>:

You need to pass the class name you want to toggle as a parameter. Otherwise you’ll toggle all the classes on the element (eg remove them) meaning the the selector will no longer work... Add another class (or id) to your element and call using that

$(".toggle").toggleClass("dark")

jQuery toggleClass(), jQuery toggleClass() for beginners and professionals with examples of jQuery effects, toggleCLass() method is used to add or remove one or more classes from the selected elements. This method toggles between adding and removing one or more class name. function (index, currentclass), It is an optional parameter. Toggle between adding and removing a class name How to use the toggleClass() method to toggle between adding and removing a class name. Toggle classes using a function Using a function to specify which class names should be toggled for the selected elements. Using the switch parameter How to use the switch parameter to only add or remove a

.toggleClass(), Description: Add or remove one or more classes from each element in the set of One or more class names (space separated) to be toggled for each element in the A boolean value to determine whether the class should be added or removed. toggleClass() method may not fail directly, as the method still exists. In the first version, if an element in the matched set of elements already has the class, then it is removed; if an element does not have the class, then it is added. For example, we can apply .toggleClass() to a simple <div> :

jquery toggle classes, jQuery also provides an animation method named .toggle() that toggles the when clicked - georgeadamson/jquery-toggle-class If a class does not exist in that toggleCLass() method is used to add or remove one or more classes from the� Posted in: Using jQuery Core > Frequently Asked Questions How do I determine the state of a toggled element? You can determine whether an element is collapsed or not by using the :visible and :hidden selectors.

Toggle the class, toggleClass( className ) Returns: jQuery. Description: Add or remove one or more classes from each element in the set of function(index, class, switch)A function that returns class names to be toggled in has the class, then it is removed; if an element does not have the class, then it is toggleClass() to a simple <div> : If you want to toggle between adding and removing classes from selected elements then use the jQuery toggleClass method. Note that if an element does not have a particular class then this method will add that class to the element. Else if the element already has that class then.toggleClass () will remove that class from the element.

Comments
  • What class is it supposed to toggle?
  • In your case if you want to remove the dark class, you could either use $('.dark').removeClass('dark'); or $('.dark').toggleClass('dark'); (which would simply have an additional internal if($(this).hasClass('className') check).
  • It is supposed to toggle the .dark class.
  • Hi Patrick and welcome to SO. It seems the author specifically wants to target the .dark selector in his post whereas your code would switch the class of the HTML body. I don't think this is what the question author is looking for.
  • From the CSS he provided, I guessed that ".dark" is the theme-class he wants to be toggled.
  • You are right, I had overlooked that. So your answer would be perfectly correct.
  • The class provided inside the toggleClass(className) does not have the CSS class dot in front as it only and specifically handles classes. I already fixed this for you in your code.
  • Cheers. Was editing myself when you fixed. On phone and have fat fingers :D