Onkeyup doesn't work

my expected result

As I type in the input text box, I get the instant amount in the third text box

if i type 3 in the first text box and the key is up, instantly 3 + 0 = 3 should show up in the third box

but does not show the result

     $(document).ready(function(){
        var a=0, b=0,c=0;

        $("input:#a").keyup(function(){
            a=$("input:#a").val();
            c=a+b;
            $("input:#c").append(a+"+"+"b"+"="+c);
         });

        $("input:#a").keyup(function(){
            b=$("input:#b").val();
            c=a+b;
            $("input:#c").append(a+"+"+b+"="+c);
        });

     });



        <tr><td><input type="text" id="a"></td></tr>
        <tr><td><input type="text" id="b"></td></tr>
        <tr><td><input type="text" id="c"></td></tr>

      

+3


source to share


4 answers


You need something along the following lines:

<table>
  <tbody>
    <tr><td>A<input type="text" id="a" /></td></tr>
    <tr><td>B<input type="text" id="b" /></td></tr>
    <tr><td>C<input type="text" id="c" /></td></tr>
  </tbody>
</table>

<!-- If you need this: -->
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>

$(document).ready(function(){
    var a=0, 
        b=0;

    $('input#a').bind('keyup', function(e){
        a = parseInt($(this).val()) || 0;
        var c = (a + b);

        $('input#c').val(a + '+' + b + '=' + c);
    });  

    $('input#b').bind('keyup', function(e){
        b = parseInt($(this).val()) || 0;
        var c = (a + b);

        $('input#c').val(a + '+' + b + '=' + c);
    });

});

      



This code performs the calculation and puts the result in field C and automatically updates C with the result of the calculation.

+1


source


You are concatenating strings. You need to actually add numbers (and not use .append

):

$("#c").text(parseInt(a) + parseInt(b));

      



Also, you should write like ).on('keyup', function () {

, not).onkeyup

0


source


Correct term .keyup()

instead of .onkeyup

.

http://api.jquery.com/keyup/

0


source


try it

$(document).ready(function () {
    var a = 0,
        b = 0;
    $("input#a").on('keyup', function () {
        a = parseInt($("input#a").val());
        $("input#c").val(parseInt(a + b););
    });

    $("input#a").on('keyup', function () {
        b = parseInt($("input#b").val());
        $("input#c").val(parseInt(a + b););
    });

      

0


source







All Articles