Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onkeyup not working

Tags:

jquery

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>
like image 805
violet kiwi Avatar asked May 15 '26 22:05

violet kiwi


1 Answers

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.

like image 194
Seer Avatar answered May 18 '26 12:05

Seer