Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best alternative for jQuery toggle() method?

jQuery deprecated toggle() method. It was way to easy to toggle classes, While searching stackoverflow I found various other methods to accomplish the same task( Alternative to jQuery's .toggle() method that supports eventData? ). Which jQuery snippet should be ideal to switch classes for this markup?

Thanks

Markup:

<a href="#">Toggle Class</a>
<div class="a"></div>

CSS:

.a{
background: #f2f2f2;
}
.b{
background: #ededed;
}
like image 398
Rajender Joshi Avatar asked Aug 01 '26 14:08

Rajender Joshi


1 Answers

In this case, simply use .toggleClass().

$("a").click(function(){
    $("div").toggleClass("a b");
});
like image 184
Kevin B Avatar answered Aug 03 '26 04:08

Kevin B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!