skip to Main Content

My cart is incremented using an ajax call. I want to add a class when the counter is 1 and remove a class when the counter is 0. Can someone tell me how can I achieve this without a page load? Is there any ajax query which fires automatically when the counter increments and decrements?
https://prnt.sc/1xaim0a
https://prnt.sc/1xaipj8

2

Answers


  1. Chosen as BEST ANSWER

    $(document).on("click", function () {
      var count_new = 0;
      var subtotal_new = 0;
      setTimeout(function () {
        jQuery.getJSON('/cart.js', function (data) {
          count_new = data.item_count;
          if (parseInt(count_new) == 0) {      
            $(".Cart_click").find(".mobile-icons").find(".hotiya").removeClass("cart_ping")
          } else {
            $(".Cart_click").find(".mobile-icons").find(".hotiya").addClass("cart_ping")
          }
        });
      }, 500);
    });

    I devised a solution.


  2. Why not check, after the ajax call, the value of counter and update according that the class? This is an exemple of ajax callback:

    function (data) {
        if(data.counter == 0){
             $('#id').removeClass('the-class');
        }else{
             $('#id').addClass('the-class');
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search