Multiple Condition In .each JQuery
Here is my HTML
Solution 1:
As simple as one line with toggleClass
:
$('.sp-right, .sp-left').toggleClass("sp-right sp-left");
No need to check for classes and remove/addClass manually, as toggleClass
does exactly this. You can also provide multiple space separated classes to toggle.
Solution 2:
Check the current classname inside the function.
$(".sp-left, .sp-right").each(function(){
var el = $(this);
if (el.hasClass('sp-left')) {
el.removeClass("sp-left").addClass("sp-right");
} else {
el.removeClass("sp-right").addClass("sp-left");
}
});
Post a Comment for "Multiple Condition In .each JQuery"