tags:

views:

38

answers:

3

Hi,

I was wondering if someone can please help me with a jQuery script. I am using MVC 2.

I have 2 textboxes. When values are typed into the textboxes then the sum of these values should display in a label control as the user types. So if the page loads then the textboxes are empty so the label must also be empty (blank). When the user types in a value like 10 then 10 should display in the label. If the user types 5 into the other textbox then 15 should display. If the user clears the 2 textboxes then the label should be blank again.

I hope someone can help here :)

Thanks

+1  A: 
$('#textbox1, #textbox2').keyup(function() {

   $('#label').html('');

   var val1 = $('#textbox1').val();
   var val2 = $('#textbox2').val();

   if(!isNaN(val1) && !isNaN(val2)) {

      $('#label').html( parseInt(val1, 10) + parseInt(val2, 10) );

   }
});

UPDATE

To accommodate your comment, see the changes below. Having made this change, I'm starting to lean towards Reigel's answer being the way to go, but perhaps with modification for the 0 result always being represented as an empty string.

$('#textbox1, #textbox2').keyup(function() {

   $('#label').html('');

   var val1 = $('#textbox1').val();
   var val2 = $('#textbox2').val();

   if(!isNaN(val1) || !isNaN(val2)) {

      $('#label').html( (parseInt(val1, 10) || 0) + (parseInt(val2, 10) || 0) );

   }
});
David Hedlund
Thanks. I'm new to jQuery. Does this code have to be in $(document).ready(function () {})?
Brendan Vogt
@Brendan Vogt: it's usually the best place to attach all the event handlers (e.g. `keyup` in your case), so yes, I would place it in the `$(document).ready()` function body
Giu
@Brendan: yes, putting it inside that function means that the code is not executed until the DOMReady event is fired, which is the first time you're guaranteed to have access to all the DOM nodes in the page. It is always best practice to defer anything that's accessing DOM nodes to DOMReady.
David Hedlund
@David: Your code only works if there is a number in all the textboxes. I want it to calculate if there is a value in any of the textboxes. Lets say you only type a value in the first textbox and nothing in the second textbox then the result must be equal to the value in the first textbox.
Brendan Vogt
@Brendan: see my update.
David Hedlund
@David: Thanks. Just a final question, if you typed values in and made all the textboxes blank then the value of the label is 0. I want it blank, is there a shorter way of doing this or do I need to create a total variable, if it is 0 then make label blank?
Brendan Vogt
@David: I changed parseInt(val1, 10) || 0) to parseInt(val1, 10) || ''), it works but is it best practices?
Brendan Vogt
+1  A: 
$("#textBox1").keydown(function(event) {
   updateLabel();
});
$("#textBox2").keydown(function(event) {
   updateLabel();
});
function updateLabel()
{
   var val = 0;
   if(isNaN($("#textBox1").val()) && isNaN($("#textBox1").val()))
   {
       $("#myLabel").val("");
       return;
   }
   if(!isNaN($("#textBox1").val()))
       val += $("#textBox1").val();
   if(!isNaN($("#textBox2").val()))
       val += $("#textBox2").val();
   $("#myLabel").val(val); 
}
Spencer Ruport
+1  A: 
$(document).ready(function() {
    $('#num1, #num2').keyup(function(){
        var num1 = parseInt($('#num1').val(),10) || 0;
        var num2 = parseInt($('#num2').val(),10) || 0;
        $('label.sum').text((num1 + num2)|| '')
    });
});​

fair demo

Reigel
This makes a lot of assumptions. Anything that can be `parseInt`:d will be, and anything that can't will be treated as `0`. Hence `'a1' + '1a'` will yield `1`. This also doesn't distinguish the result `0` an erroneous result. Hence `'1' + '-1'` yields `''`.
David Hedlund
Oh, turns out the first assumption I mentioned seems to be quite what OP wants :)
David Hedlund