jquery - How to best update a text file on change -


i have jquery script on page that's coming together, code , people here, , i'm stuck @ point need call part of larger function. i'm not sure how break multiple functions think. runs correctly when create dynamic input fields , totals amount fields totals box, if don't create new input boxes don't part need total these inputs. here sample of have far comment in code need help:

http://jsfiddle.net/a3dnr/

// ** need call part when text entered/changed in of 'amount' fields **    var total = 0;     $('input[name^="amount"]').each(function () {         total += parseint(this.value, 10) || 0;         if (this.value.length === 0) this.value = 0;     });     $('#totals').val(total); 

the code block calculates totals inside click handler add new row button it'll called when button pressed. need move outside of click handler. check fiddle. i've converted function following

        function updatethetotal(){            var total = 0;            $('input.amount').each(function () {               total += parseint(this.value, 10) || 0;               if (this.value.length === 0) this.value = 0;            });            $('#totals').val(total);         } 

and gets called on keyup of amount fields

$('.amount').on('keyup', updatethetotal); 

note: code candidate optimization caching selectors


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -