Aviso
Aviso

Reputation: 695

Jquery addClass() , removeClass() functions does not work

$('.push').each(function(){
  if($(':first-child',this).hasClass( "activex" )){
    $(this).off().off('click').on('click',function(){
      var a = $(this).attr('id');
      $.ajax({
        type: "POST",
        url: "includes/rcart.php",
        data:{'pid': a},
        success: function(data){
          var a= parseInt($('.cart').text());
          if((data.indexOf("2")) >= 0){
            console.log(data);
            console.log('not removed'); 
          }else{
            a--;
            console.log('removed');
            $(this).children().removeClass('activex');
            $('.cart').text(a);
          }
        }
      });
      console.log(a); 
    });
  }else{
    $(this).on('click',function(){
      var a = $(this).attr('id');
      $.ajax({
        type: "POST",
        url: "includes/cart.php",
        data:{'pid': a},
        success: function(data){
          console.log(data);
          var a= parseInt($('.cart').text());
          if((data.indexOf("2")) >= 0){
            console.log('done'); 
          }else{
            a++;
            $('.cart').text(a);
            $(this).children().addClass('activex');
          }
        }
      });
      console.log(a); 
    });
  }
});

I am trying to remove the class activex whenever the button with class push has been clicked but its not working and there's no error in the code and it's not removing the class. If i remove the class manually in the chrome console with class .push it works.

I am trying to remove the class activex whenever the button with class push has been clicked but its not working and there's no error in the code and it's not removing the class. If i remove the class manually in the chrome console with class .push it works.

Upvotes: 2

Views: 87

Answers (1)

Sanjay Kumar Singh
Sanjay Kumar Singh

Reputation: 937

Here is your updated code, just i have removed children() from your code. let me know if it helped or not.

$('.push').each(function(){


      if($(':first-child',this).hasClass( "activex" )){
          $(this).off().off('click').on('click',function(){
        var a = $(this).attr('id');
                $.ajax({

    type: "POST",
    url: "includes/rcart.php",
    data:{'pid': a},
    success: function(data){


     var a= parseInt($('.cart').text());

     if((data.indexOf("2")) >= 0){
          console.log(data);
         console.log('not removed'); 
     }
     else {
     a--;
     console.log('removed');
     $(this).removeClass('activex');
     $('.cart').text(a);
     }
    }

    });

        console.log(a); 
    });

      }

      else {

    $(this).on('click',function(){
        var a = $(this).attr('id');
                $.ajax({

    type: "POST",
    url: "includes/cart.php",
    data:{'pid': a},
    success: function(data){

    console.log(data);
     var a= parseInt($('.cart').text());

     if((data.indexOf("2")) >= 0){
         console.log('done'); 
     }
     else {
     a++;
     $('.cart').text(a);
     $(this).addClass('activex');
     }
    }

    });

        console.log(a); 
    });
      }

});

Upvotes: 1

Related Questions