views:

35

answers:

1

I'm trying to use jquery to loop through a block of forms that contain numeric data and add it all up to be displayed in a form with id grand. I have the ability to dynamically add new forms, so the number of forms I'm looping through will change. Here's my jquery code I wrote that isn't working for whatever reason:

$('#table').delegate('input#grand', 'focus', function(){
var grand = $(input.expend).map(function(){
  return this.value;
});
$(grand).each(function(){
  var g = 0;
  g+= $(this);
  $('input#grand').val(g);
});

});

A: 

In my example, I'm using a table with the ID of "table" as the container for the input elements, but you can use whatever you want:

    <script type="text/javascript">
        $(document).ready(function () {
            $("#table input").change(function () {
                var total = 0;
                $("#table input").each(function (index, item) {
                    var value = parseFloat($(item).val());
                    if (!isNaN(value)) {
                        total += value;
                    }
                });
                $("#grand").val(total);
            });
        });
    </script>

    <table id="table">
        <tr>
            <td>
                <input />
            </td>
        </tr>
        // more inputs
    </table>
    <input id="grand" />
Doc Hoffiday