Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass a variable inside a jquery function $.each($("abc")...?
    primarykey
    data
    text
    <p>I'm trying to iterate a bunch of SELECT OPTION html drop-down fields and from the ones that are NOT empty, take the values and add a hidden field for a PAYPAL shopping cart.</p> <p>My problem is that for some reason, the variable "curitem" is not passed inside the <code>each</code> function and I can't add the hidden field like they should. All I get is "NaN" or "undefined".</p> <p>What PAYPAL expects is: item_name_1, item_name_2, etc. All numbers must iterate by +1.</p> <p>How can I do this? </p> <p>Thanks a bunch in advance</p> <pre><code>var curitem; $.each($("select"), function(index, item) { var attname = $(this).attr("name"); var nom = $(this).attr("data-nom"); var prix = $(this).attr("data-val"); var partname = attname.substring(0, 1); var qte = $(this).val(); // i want all my &lt;select option&gt; items that the NAME start with "q" AND have a value selected if (partname == "q" &amp;&amp; isNaN(qte) == false &amp;&amp; qte &gt; 0) { // item name var inp2 = document.createElement("input"); inp2.setAttribute("type", "hidden"); inp2.setAttribute("id", "item_name_"+curitem); inp2.setAttribute("name", "item_name_"+curitem); inp2.setAttribute("value", nom); // amount var inp3 = document.createElement("input"); inp3.setAttribute("type", "hidden"); inp3.setAttribute("id", "amount_"+curitem); inp3.setAttribute("name", "amount_"+curitem); inp3.setAttribute("value", prix); // qty var inp4 = document.createElement("input"); inp4.setAttribute("type", "hidden"); inp4.setAttribute("id", "quantity_"+curitem); inp4.setAttribute("name", "quantity_"+curitem); inp4.setAttribute("value", qte); // add hidden fields to form document.getElementById('payPalForm').appendChild(inp2); document.getElementById('payPalForm').appendChild(inp3); document.getElementById('payPalForm').appendChild(inp4); // item number curitem = curitem + 1; } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload