violet kiwi
violet kiwi

Reputation: 705

onkeyup not working

my expected output is

As i type on input text field i get instant sum on third text field

if i type 3 on first text field and key is up , instantly 3+0=3 have to show on third field

but doesnt show any 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>

Upvotes: 3

Views: 3586

Answers (4)

Seer
Seer

Reputation: 5237

You need something along the lines of the following:

<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 box C, and automatically updates C with the result of the calculation.

Upvotes: 1

Codegiant
Codegiant

Reputation: 2150

Try this

$(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););
    });

Upvotes: 0

Ladineko
Ladineko

Reputation: 1981

The correct term is .keyup() instead .onkeyup.

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

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191749

You're concatenating strings. You need to actually add the numbers (and not use .append):

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

Additionally, you should write as ).on('keyup', function () { rather than ).onkeyup

Upvotes: 0

Related Questions