tags:

views:

360

answers:

1

Trying to figure out how to write a jquery formula that will sum all input fields that begin with "pull" on keyup... I'm trying the code below, but nothing happens... No errors, and no updates either.... (the html is at the very bottom)

$(document).ready(function(){
    /* sums pull total input fields */
    $("input[name^='pull']").bind("keyup", "calcPullTotal");
    calcPullTotal();
});


function calcPullTotal() {
    $("[id=totalpull]").calc(
        "pullnum + 0", { pullnum: $("input[name^=pull]") },
        function (s){
            return s.toFixed(0);
        },
        function ($this) {
            var sum = $this.sum();
                $("#totalpull").text(
                sum.toFixed(0)
            );
        }
    );  
}
<table id="convert">
<tbody>
<tr><td><input type="text" value="" name="pull0" /></td></tr>
<tr><td><input type="text" value="" name="pull1" /></td></tr>
<tr><td><input type="text" value="" name="pull2" /></td></tr>
<tr><td><input type="text" value="" name="pull3" /></td></tr>
</tbody>

<tfoot><tr><td><input type="text" id="totalpull" name="totalpull" value="" /></td></tr></tfoot>
</table>
+1  A: 

Try:

$("input[name^='pull']").bind("keyup", calcPullTotal);
calcPullTotal();

You were passing the string "calcPullTotal" as the second argument to bind, which expects a function.

karim79
No, that didn't work either... That's how I had it at first, but still nothing...
whitman6732
I'm assuming you didn't intend for me to take out of document.ready?
whitman6732
No - don't take it out of document.ready
karim79
That didn't work. It occurred to me that maybe because the input elements are within a table cell, I'd have to do something different? I did try table > tbody > tr > td > input["name^=pull"], but didn't seem to have an effect... In any event, I updated the original post...
whitman6732