2

I am trying to have a simple addition calculation when an input is keyup'ed. Here is my html:

<tr id="a">
        <td>
            <label>A</label>
        </td>
        <td>
            <label>Results/Action Orientation (asset)</label>
        </td>
        <td class="shade">
            <input id="a_per" type="text" />
        </td>
        <td class="shade">
            <input id="a_pot" type="text" />
        </td>
        <td class="shade">
            <input id="a_tot" class="totals" type="text" disabled="disabled" />
        </td>
</tr>

And here is my jQuery for the keyup event:

$('#a_per').keyup(function() {
    if($('#a_pot').value != "") {
        var tot = this.value + $('#a_pot').value;
        $('#a_tot').value = tot;
    }
});

I am going to tailor it to fire when either #a_per or #a_pot keyup but first I want to make sure this will work.

Any help is appreciated. Thanks.

3 Answers 3

2

Another alternative:

$('#a_per,#a_pot').keyup(function() {
  var a_per = +$('#a_per').val() || 0;
  var a_pot = +$('#a_pot').val() || 0;
  $('#a_tot').val(a_per + a_pot);
});​

A working example.

Sign up to request clarification or add additional context in comments.

6 Comments

Wouldn't this still need casting to int/float?
You could say this autocast itself. Being honest I think it is the better answer.
I am not a javascript/jQuery expert so excuse my ignorance. What are the advantages of this method over those listed above? If it is more efficient I will use it. It isn't as readable as the above examples though.
Will do, thanks for the advice and help. I have been looking at your answer more and agree with you now. I didn't notice all that was going on. I am switching yours to the answer. Thanks again. :)
|
1

Change your use of .value to .val(). You'll also want to apply parseFloat or parseInt to your addition function, otherwise you'll end up concatenating strings. Also wrap this is jQuery tags like:

$('#a_per').keyup(function() {
    if($('#a_pot').val() != "") {
        var tot = parseFloat($(this).val()) + parseFloat($('#a_pot').val());
        $('#a_tot').val(tot);
    }
});​

5 Comments

Awesome! This fixed it. I will set to answer once stackoverflow lets me. Thanks a lot. :)
No problem. You may also want to consider checking for non-numeric input.
I will be adding validation to it yet. I switched over to parseInt as I will not be accepting any floats. I assume that parseInt is faster than parseFloat, although it would probably not be noticable.
Not noticeable by much. When using parseInt, make sure you add the radix parameter. E.g. parseInt($(this).val(),10).
Forgot about that. Thanks. Set to answer now.
1

use val(), not value.

$(function(){

   $('#a_per').keyup(function() {
     var item=$(this);

      if(item.val() != "") {
         var tot = parseInt(item.val()) + parseInt($('#a_pot').val());
         $('#a_tot').val(tot);
      }
   });

});

Working sample : http://jsfiddle.net/zzS3U/9/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.