javascript - Jquery : Product between 2 lists of inputs into a 3rd list -
i don't have experience in jquery, i'm facing following challenge
i have following table
<table> <thead> <tr> <td>qty</td> <td>description</td> <td>unit price</td> <td>total price</td> <tr> </thead> <tbody> <tr id="itemrow"> <td><input type="text" name="quantity"/></td> <td><input type="text" name="description"/></td> <td><input type="text" name="unitprice"/>/td> <td><input type="text" name="totalprice"/></td> <tr> </tbody> </table> <input type="text" name="total"/>
additionally, i'm able clone #itemrow many times required, enlarging amount of items.
the idea calculate total price each row (by quantity
* unitprice
) , assign totalprice
. , assign sum of totalprice
total
.
this javascript i'm using, error says "cantidades[i].val() not function"
function calculate(){ var $total = $('#total'); var $cantidades = $('input[name=quantity]') var $unitarios = $('input[name=unitprice]') var $totales = $('input[name=totalprice]') var len = $cantidades.length; $total.val(0); (var = 0; < len; i++){ // calculate total row. error here! $totales[i].val($cantidades[i].val() * $unitarios[i].val()); // accumulate total $total.val($totalexento.val() + $totales[i].val()); } }
what missing? think i'm not getting "jquery objects" selector i'm not sure hot this.
thanks in advance!
this line: var $cantidades = $('input[name=quantity]')
retrieves jquery instance cannot accessed did $cantidades[i]
.
fix this:
var singleelementcantidades = $($cantidades[i]); singleelementcantidades.val();
what happened $('input[name=quantity]')
retrieves array instance of jquery. , when access content using cantidades[i] not managing jquery instance anymore, accessing else wich doesn't have definition of val.