jQuery .toggle event deprecated, What to use?

user1834464 picture user1834464 · Jul 11, 2013 · Viewed 10k times · Source

Since the jQuery .toggle event method is deprecated. What are we suppose to use to simulate this event (alternate clicks)?

Answer

Jay Lane picture Jay Lane · Jul 11, 2013

Add this outside of document.ready

$.fn.clicktoggle = function(a, b) {
    return this.each(function() {
        var clicked = false;
        $(this).click(function() {
            if (clicked) {
                clicked = false;
                return b.apply(this, arguments);
            }
            clicked = true;
            return a.apply(this, arguments);
        });
    });
};

then use the following to replicate the .toggle functionality:

$("#mydiv").clicktoggle(functionA,functionB);

Found on the JQuery Forums