jQuery .toggle event deprecated, What to use?
Asked Answered
U

2

10

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

Ursel answered 11/7, 2013 at 0:35 Comment(7)
Can you provide code?Milesmilesian
Duplicate of #2459653Too
@jahroy I don't want an animation method...Ursel
I did read the documentation and the toggle() animation method toggles the visibility of an element, but that's not what I want.Ursel
Maybe this will help. (sorry I mis-read your question at first)Zephyrus
I want a toggle event. Mr Jay Lane gave me a good answer. Thanks anyway!Ursel
Okay, I will look at that.Ursel
S
9

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

Sherrard answered 11/7, 2013 at 0:37 Comment(4)
Sorry, but what exactly is this? Is it a function you wrote? Why $.fn.clicktoggle and how do you call this function? Thank you for your anwser!Ursel
The above code extends the jQuery object by adding a new method. You would add that code to your "on ready" handler. Then you can invoke clicktoggle() as if it was a jQuery method. You can read about adding a jQuery method here.Zephyrus
no problem, if it worked for you if you can add it as solved that would be greatSherrard
Yes, I have to wait 4 minutes :).Ursel
A
-1
var i = 0;    
$('button').click(function() { 
    if (i == 0){        
        $('div').css({background: 'red'}) 
        i++; 
    } else { 
        $('div').css({background: 'yellow'}) 
        i = 0; 
    }
});
Aconcagua answered 27/6, 2014 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.