Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda