Since the jQuery .toggle event method is deprecated. What are we suppose to use to simulate this event (alternate clicks)?
jQuery .toggle event deprecated, What to use?
Asked Answered
Can you provide code? –
Milesmilesian
Duplicate of #2459653 –
Too
@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
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
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 great –
Sherrard
Yes, I have to wait 4 minutes :). –
Ursel
var i = 0;
$('button').click(function() {
if (i == 0){
$('div').css({background: 'red'})
i++;
} else {
$('div').css({background: 'yellow'})
i = 0;
}
});
© 2022 - 2024 — McMap. All rights reserved.