I have some code that someone gave me that sums the contents of all
textboxes with the class "additem, and puts the sum in the textbox
with the class "totalitem".  I am not a javascript or jQuery Guru by
any means, but here it is:

$(document).ready(function() {
        var total = 0;
        var initialText;
        //add a focus event to be able to determine if the text has
changed.
        $(".additem").focus(function() {
            initialText = this.value;
        });

        //now we evaluate if the text has changed, and if so, update
the total.
        $(".additem").blur(function() {
            if (this.value != initialText) {
                //get the Total currently
                total = parseInt($(".totalitem").html());
                //subtract the amount we're changing
                total -= initialText;
                //add in the new amount
                total += parseInt(this.value);
                //update the total
                $(".totalitem").html(total);
            }
        });

        $(".additem").each(function() {
            total += parseInt(this.value);
        });
        $(".totalitem").html(total);
    });

How could this be modified to allow summing multiple sets of textboxes
(using each set using a different class name) into multiple total
textboxes (again using different class names)?  Can this somehow be
converted to a function, or can the selectors be dynamically modified
some way?

Any help or advice will be appreciated!

Reply via email to