I wrote a simple calculator, after inserting a number into it, it does not perform any operations until I click on an empty space.

Is there no way to display the result as soon as I enter both numbers? Maybe there is a way to run a calculator on a keyboard event like that? tell me how to implement?

(it seems to be there, I just noticed that when I enter the text here, it is broadcast at the bottom without having to click the mouse)

$('.first,.second').change(function() { var a = parseInt($('.first').val()); var b = parseInt($('.second').val()); c = a * b; if (!c) { $('.result').val('0'); } else { $('.result').val(c); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <input class='first' placeholder="введите число"> <input class='second' placeholder="введите число"> <input class='result' readonly="readonly" placeholder="рузультат"> 

    1 answer 1

    You have a change event that does not occur until <input> loses focus.

    Another thing is the HTML5 event "input" :

     $('.first,.second').on('input', function() { var a = parseInt($('.first').val()); var b = parseInt($('.second').val()); c = a * b; $('.result').val( c ? c : '0'); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <input class='first' placeholder="первое число"> <input class='second' placeholder="второе число"> <input class='result' readonly="readonly" placeholder="произведение чисел"> 

    • Do not tell me the Russian-language article, where you can read about all the events, and then I write half a year and find out all the new ones. - Evgeny Shevtsov
    • just added just the link to the description of the event, but in English. Probably all events are listed on the MDN. - Sergiks