Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
Why does this JS code only work once on load

So I've been loosing my mind with this issue for well over a day now, I've written a simple API call in liquid to fetch the stock count for items in cart, this works fine...

My issue is in trying to use said data on the front end, below is fairly basic JS/JQUERY, for some reason the code works fine on page load but any other time the code is triggered e.g. on cart popup or add to cart the get request just returns undefined, there are no console errors.

API call data

{39634393366699 :1}

HTML ELEM

<p class="cart-stock-message" data-cart-qty="7" data-variant-id="39634393366699"></p>

JS

 $(".cart-stock-message").each(function(){
                          
                          var varID = $(this).data("variant-id");
                          var cartQty = $(this).data("cart-qty");
        
                          var message = $.get("/pages/stockdata", function(data, status){
                            
                            var cartItemMaxQuantities = data;
                            
                             for(var id in cartItemMaxQuantities){
                              if(id == varID){
                                var count = cartItemMaxQuantities[id];
                                if(cartQty > count){
                                 return "Note: " + count + " available for immediate dispatch, the remainder will be placed on back order.";
                                }
                              }
                            }
                          });                         
                          
                        $(this).text(message);

Whenever I call that JS code, whether its in a function or via an event listener it fails.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda