Since the jQuery .toggle event method is deprecated. What are we suppose to use to simulate this event (alternate clicks)?
            Asked
            
        
        
            Active
            
        
            Viewed 1.0k times
        
    10
            
            
        - 
                    Can you provide code? – Dom Jul 11 '13 at 00:37
- 
                    Duplicate of http://stackoverflow.com/questions/2459153/alternative-to-jquerys-toggle-method-that-supports-eventdata – Jack Daniel's Jul 11 '13 at 00:39
- 
                    @jahroy I don't want an animation method... – Jul 11 '13 at 00:42
- 
                    I did read the documentation and the toggle() animation method toggles the visibility of an element, but that's not what I want. – Jul 11 '13 at 00:44
- 
                    Maybe [this](http://stackoverflow.com/q/14301935/778118) will help. (sorry I mis-read your question at first) – jahroy Jul 11 '13 at 00:46
- 
                    I want a toggle event. Mr Jay Lane gave me a good answer. Thanks anyway! – Jul 11 '13 at 00:47
- 
                    Okay, I will look at that. – Jul 11 '13 at 00:47
2 Answers
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
 
    
    
        Jay Lane
        
- 1,379
- 15
- 28
- 
                    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! – Jul 11 '13 at 00:41
- 
                    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](http://stackoverflow.com/q/12093192/778118). – jahroy Jul 11 '13 at 00:44
- 
                    no problem, if it worked for you if you can add it as solved that would be great – Jay Lane Jul 11 '13 at 00:47
- 
                    1
-1
            
            
        var i = 0;    
$('button').click(function() { 
    if (i == 0){        
        $('div').css({background: 'red'}) 
        i++; 
    } else { 
        $('div').css({background: 'yellow'}) 
        i = 0; 
    }
});
 
    
    
        Quinn Comendant
        
- 9,686
- 2
- 32
- 35
 
    
    
        Pomme Zede
        
- 11
- 1
