Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!