0

My first time on stackoverflow. I tried to find an answer on previous posts but still looking for a solution.

I try to get the order delay displayed for each items on my product list.

I'm not able to get the proper delay for each product, only the first delay is assigned on all products.

PS: The number of products is variable.

<html>
    <head>        
       <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>    
       <script>
          $(document).ready(function() {  
          var skuDelay = $("div").attr("orderDelay");  
                  $("p").after("<br>DELAY "+ skuDelay +" DAYS");

              console.log("skuDelay = " + skuDelay)
          });        
       </script>

    </head>
    <body>
        <div orderDelay="4-6"><h3>Product 1</h3>
            <p>Price = 10.40$</p>       
        </div>
        <br><br>
        <div orderDelay="8-10"><h3>Product 2</h3>
            <p>Price = 9.30$</p>       
        </div>
        <br><br>
        <div orderDelay="14-21"><h3>Product 3</h3>
            <p>Price = 14.40$</p>       
        </div>    
    </body>
</html>

This is HTML Result =

Product 1 Price = 10.40$ DELAY 4-6 DAYS

Product 2 Price = 9.30$ DELAY 4-6 DAYS

Product 3 Price = 14.40$ DELAY 4-6 DAYS

Thanks!

  • 1
    `$("p").after(...)` will select every `p` element and add to each one. And generally if you're adding your own attributes, you should look at [data-attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes), which jQuery [handles nicely](https://api.jquery.com/data/) – DBS Jan 28 '20 at 14:40
  • My limitation is a really poor CMS working with jquery-1.11.0 What you suggest to me is earlier and not possible for me to use it. Thanks for your suggestion. – Philippe Jan 28 '20 at 15:08
  • I solved my problem Using info on -https://stackoverflow.com/questions/1074728/jquery-pass-variable-to-eq-does-not-work -https://stackoverflow.com/questions/1442925/how-to-get-nth-jquery-element I changed my code for... $(document).bind('ready ajaxStop',function(){ var isLeadtime = $("div").length; for( i=0; i<= isLeadtime; i++){ var skuDelay = $("div:eq("+i+")").attr("orderDelay"); $("div:eq("+i+")").after("
    DELAY "+ skuDelay+ " DAYS"); }});
    – Philippe Jan 28 '20 at 17:30

0 Answers0