Search code examples
jqueryclasstoggle

The proper use of jQuery toggle?


In this Stack Overflow answer

$('#user_button').toggle(function () {
    $(".hello").addClass("active");
}, function () {
    $(".hello").removeClass("active");
});

The jQuery .toggle callback function is used to add and remove classes to an element. Now according to the jQuery .toggle documentation, .toggle is originally used to switch between visibility states of the selected element.

I've made a JSFiddle demo: http://jsfiddle.net/taheri/nYdng/

In my example here it seems to only alternate between call back functions, and the visibility of the #user_button itself is not being toggled. What am i missing out in the documentation here? Can toggle be used to only alternate between functions?


Solution

  • There are two toggle methods... this one and this one. They should have named them differently to avoid this confusion.