Thursday 15 August 2013

javascript - jQuery - Update only works for one of many fields -



javascript - jQuery - Update only works for one of many fields -

i'm trying build form if update of inputs, update sum. right first input working. want utilize single class across of inputs page dynamic , id's can alter often.

here jsfiddle:

jsfiddle

here code:

var productcost = function (input, output, changeon) { var reloadcalcs = function () { var sum = 0; $(input).each(function() { sum += parsefloat($(input).val()); }); $(output).html(sum); }; $(function () { $(changeon).change(function () { reloadcalcs(); }); $(changeon).trigger('change'); }); }; productcost('.product','.sum','input');

by using

sum += parsefloat($(input).val());

you telling sum add together value of .product itself, since .product selector have multiple elements, take first one. prepare this, should utilize this:

sum += parsefloat($(this).val());

javascript jquery html

No comments:

Post a Comment