Looping through form elements in javascript and adding the fetched values
<p>I'm trying to create a simple form which calculates the values in every input field which has subtotal at the start of the name attribute:</p>

<pre><code>&lt;script type="text/javascript" src="jq.js"&gt;&lt;/script&gt;

&lt;script type="text/javascript"&gt;
$(function(){

var qty = 0;
var price = 0;
var subtotal = 0;
var qty_r = [];
var price_r = [];
var subs_r = [];
var total = 0;

$('input[name^=qty]').each(function(index) {
    qty_r[index] = $(this).val();
});

$('input[name^=price]').each(function(index){
    price_r[index] = $(this).val();
});

var j = 0;
for(j = 0; j&lt;=2; j++){

    subs_r[j] = parseInt(price_r[j]) * parseInt(qty_r[j]);

}




$('input[name^=subtotal]').each(function(index){
    $(this).val(subs_r[index]);
});


$('input[name^=qty]').keyup(function(){
    var basis = $(this).attr("id");
    var numbasis = basis.toString();

    qty = $(this).val();
    price = $('input[name=' + numbasis + ']').val();


    subtotal = parseInt(qty) * parseInt(price);

    $("#subtotal" + numbasis).val(subtotal);
</code></pre>

<p>This is the part where I'm having trouble: </p>

<pre><code>$("input[name=subs]").each(function(index){
            total = parseInt($(this).val()) + total;
            $('#total').val(total);

        });

});




});
&lt;/script&gt;
</code></pre>

<p>And here's where the data which being calculated comes from:</p>

<pre><code> &lt;?php $products = array(array("prodname"=&gt;"mais", "qty"=&gt;5, "price"=&gt;15), array("prodname"=&gt;"strawberry", "qty"=&gt;7, "price"=&gt;25), array("prodname"=&gt;"kambing", "qty"=&gt;14, "price"=&gt;3)); ?&gt;
    &lt;table border="1"&gt;
    &lt;tr&gt;
    &lt;th&gt;Product&lt;/th&gt;
    &lt;th&gt;Qty&lt;/th&gt;
    &lt;th&gt;Price&lt;/th&gt;
    &lt;th&gt;Subtotal&lt;/th&gt;
    &lt;/tr&gt;
    &lt;?php foreach($products as $key=&gt;$prods){

    ?&gt;
    &lt;tr&gt;
    &lt;td&gt;&lt;input type="text" name="prodname&lt;?php echo $key; ?&gt;" value="&lt;?php echo $prods['prodname'];  ?&gt;"/&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input type="text" id="&lt;?php echo $key; ?&gt;" name="qty&lt;?php echo $key; ?&gt;" value="&lt;?php echo $prods['qty']; ?&gt;"/&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input type="text" name="&lt;?php echo $key; ?&gt;" id="price&lt;?php echo $key; ?&gt;" value="&lt;?php  echo $prods['price']; ?&gt;"/&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input type="text" name="subs" id="subtotal&lt;?php echo $key; ?&gt;" /&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;?php } ?&gt;

    &lt;/table&gt;
Total: &lt;input type="text" id="total"/&gt;&lt;br/&gt;
</code></pre>

<p>The problem is that I'm always getting NaN as a result. </p>
