简体   繁体   中英

Get content of textarea in time

The following code always omit the last written character.

$('#input').keypress(function(event){
    var content = $(this).val();
    console.log(content);
});

input a -> output <nothing>

input ab -> output a

input abc -> output ab

etc.

Demo

I want to get the actual written characters in the textarea each time.

The keypress fires "too soon" for what you want -- you are always retrieving the previous character (thus never seeing the last character). One option, among several, is to use the keyup event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM