views:

69

answers:

4

Preamble: I'm more of a PHP/MySQL guy, just starting to dabble in javascript/jQuery, so please excuse this dumb newbie question. Couldn't figure it out from the Docs.

I have a form without a submit button. The goal is to allow the user to input values into several form fields and use jQuery to total them up on the bottom in a div. The form kinda looks like this but prettier:

<form>
Enter Value: <input class="addme" type="text" name="field1" size="1">
Enter Value: <input class="addme" type="text" name="field2" size="1">
Enter Value: <input class="addme" type="text" name="field3" size="1">
etc.....
<div>Result:<span id="result"></span></div>
</form>

Is it possible to add these up? And if so, can it be done anytime one of the input fields changes?

Thanks.

UPDATE: Brian posted a cool collaborative sandbox so I edited the code to look more like what I have and it's here: http://jsbin.com/orequ/ to edit go here: http://jsbin.com/orequ/edit

+1  A: 

Try this

$(".addme").bind("change",function(){
    var _sum = 0;

    $(".addme").each(function(i){
        _sum += parseInt($(this).val());
    });

    $("#result").val(_sum);
});

HTH Alex

AlexDuggleby
Thanks...it's not updating the span though...
joedevon
$("#result").text(_sum);
Lukáš Lalinský
Tried that too, getting NaN as the update.
joedevon
+1  A: 

Sticking this right after the </form> tag should do it:

<script>
  function displayTotal() {
    var sum = 0

    var values = $('.addme').each(function(){
      sum += isNaN(this.value) || $.trim(this.value) === '' ? 0 : parseFloat(this.value);
    });

    $('#result').text(sum);
  }
  $('.addme').keyup(displayTotal);
</script>

Here's a demo: http://jsbin.com/iboqo (Editable via http://jsbin.com/iboqo/edit)

brianpeiris
Thanks...your demo works perfectly with the code I gave you but when I add in my actual code it doesn't work. It just says NaN (which Alex' reply did too if I jiggered with it)...maybe because the real form is in a table? Or because I don't really have a div around the span? I'm going to play with it some more in that sandbox (very cool btw).
joedevon
It's probably because some of your fields are empty. It adds to NaN because parseInt(x) === NaN if x is an empty string or a string that contains non-integer characters. (See https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/parseInt). I've edited my answer to correct for empty or non-numeric values
brianpeiris
OK I'll check yours and Russ Cam's in a second. Here's my updated example just in case you miss my updated question:http://jsbin.com/orequ/
joedevon
Thanks for the help Brian! And the sandbox, that is awesome!
joedevon
Hey Brian, there's a missing semicolon, right?
joedevon
Sorry for the late reply. Yes, there's a missing semicolon after `var sum = 0`. JavaScript doesn't require semicolons, but it's always a good idea to use them in order to reduce ambiguity.
brianpeiris
+1  A: 

Any non numeric or blank values will be disregarded in the calculation (they'll be given a value of zero and hence not affect the sum).

function sumValues() {
    var sum = 0;

    $("input.addme").each(function(){
        var $this = $(this);
        var amount = parseInt($this.val(), 10);
        sum += amount === "" || isNaN(amount)? 0 : amount;
    });

    $("#result").text(sum);
}

$(function() {

  sumValues();

  $("input.addme").keyup(function(){
    sumValues();
  });

});

Working Demo

Russ Cam
I'm sure yours works too, though since Brian's did also I didn't check. Voted you up. Thanks for the help!
joedevon
+1  A: 
(function(){
    var context = $("form"), elements = $("input.addme", context);
    function getSum(elements) {
     var sum = 0;
     $(elements, context).each( function() {
      var v = parseInt(this.value);
      v === parseInt(v,10) ? sum += v : sum = sum;
     })
     return sum;
    }
    $(elements).bind("keyup", function() {
     $("#result").text( getSum(elements) );
    });
})();

isolated scope and context, included dealing with non-integer values, function getSum should rather return a value than do something itself.

wildcard
http://jsbin.com/ocedi/
wildcard