JQuery addclass / remove Class work once

I have this action on click:

$(".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");
    }    
});  

      

is there multi-step with jquery, it works great, but when i switch between steps, these codes don't work?

+3


source to share


1 answer


Correct way to use each .each (function (index, element) is the value $ (this) u will not get the element



   $(".sp-left, .sp-right").click(function(e){


      var el = $(element);

      console.log(el);

       if (el.hasClass('sp-left')) {
         el.removeClass("sp-left").addClass("sp-right");
        } else {
          el.removeClass("sp-right").addClass("sp-left");
       }    

     }); 

      

0


source







All Articles