I'm starting to get familiar with jQuery. Watching a mini video guide .

There, at the 7th minute, an example is demonstrated when we input something into input and this text is transmitted / fits into another block of code with a certain id'shnik. But when we transfer text from input with keypress, the last typed character is not transmitted. If you change the event to keyup, then everything becomes normal.

Made a simple likeness and yes, it works.

$(document).ready(function(){ $('input').keypress(function(){ $('span').text(', ' + $(this).val()) }); }); 
 <form action=""> <input type="text" placeholder="Введите текст" id="text"> </form> <p>Привет<span id="userName"></span>!</p> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 

Actually, why is the last character not "transmitted" at the keypress event? After all, keypress is when we press and release a key on the keyboard, and when we type text into input, in any case we completely press the key and release.

    1 answer 1

    After all, keypress is when we press and release a key on the keyboard.

    You try not only to watch video guides, but also to look into the documentation . Of course, you should be your first source of information.

    It can be seen that it has been the browsers, browsers, browsers, and platforms.

    The browser registers keyboard input. This is similar to the modifier and the non-printing keys such as the key. There can be no differences between the two events.

    First, it says that the event does not have a clear specification, and the behavior may vary depending on the browser. Secondly, it is indicated in black and white that the event is similar to keydown , with the exception of special keys. From here it should be obvious for what reason you get what you get, not what you fantasize.

    • ... works similar to keydown . I replaced keypress with keydown and yes, the functionality of the code has not changed, but the question still remains. Why is the last character pressed not recorded in the span ? I read the documentation: api.jquery.com/keydown there is nothing written about it - Drovosek
    • @ YegorSpiriadi is obvious, because first there is a keydown, then a change in value, and then a keyup? - teran 1:27 pm
    • that is, keydown triggered immediately after a key is pressed, but before the value is passed to input ? Where it is possible to learn more about it? - Drovosek
    • one