简体   繁体   中英

Detecting user typing in a text box

I am using listeners to detect when a user finishes with textbox; when the text is value is changed, I am pasting their text on to the page using innerHTML.

document.getElementById("dateinput").addEventListener("change", function() {
     myDate(document.getElementById("dateinput").value);
     }, false);

Is there a way to listen for each key press to immediately paste the .value character by character as they type? I only want to do this while the textbox has focus.

Thanks.

Yes, the events are keyup , keydown , keypress or, in contemporary browsers, input (which covers any user-initiated, non-programmatic, event in the relevant <input> element).

References:

you can use an of keyup/keydown/keypress

document.getElementById("dateinput").addEventListener("keyup", function() {
     myDate(document.getElementById("dateinput").value);
     }, false);

you can use onpaste

 document.getElementById("dateinput").addEventListener("paste", function() {
         myDate(document.getElementById("dateinput").value);
         }, false);

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