I want text I enter in a text field immediately to be shown in a div:
function func() {
document.getElementById("query").innerHTML = document.getElementById("keyword").value;
}
window.onload = function() {
keyword.onkeydown = function(e) {
func();
}
}
<input type="text" id="keyword" size="40">
<div id="query"></div>
It works, the only problem is, it is displayed with a delay.
So if I enter "abc", it only shows "ab". I need to enter another character, for example "abcd", so that it shows "abc".
The last character is always missing.
Here you can try it out: http://jsfiddle.net/285cz0np/
Delayed text messages is a real issue especially if the message is time sensitive. It can cause you a lot of trouble especially when these text messages are work-related and came from your superior. The problem of delayed text messages doesn’t actually seem to be caused by a specific messaging application.
You may experience delays when typing if your PC’s keyboard driver is corrupt or incompatible with the keyboard. Fix this by uninstalling the driver; Windows will install a fresh copy afterward. 1. Launch the Device Manager, right-click the keyboard driver, and select Uninstall device. 2. Select Uninstall on the confirmation prompt to proceed. 3.
Aside from sending a message to your own number, you should also test its sending functionality by sending a message to that of another number beside yours. Sometimes the problem of delayed messages or error in messages happens at one point in time only.
If you’re using a wired external keyboard, make sure the cable is tightly plugged into your PC’s USB port. Unplug the keyboard and plug it back into the port, or switch the keyboard to a different USB port. You could also use the keyboard on another computer. If the typing delay persists, the keyboard is probably faulty.
The event is being fired onkeydown
. Since the value of the input element hasn't changed yet, you aren't seeing the results.
You could change it to onkeyup
, instead: (updated example). Or you could set a delay before calling the function.
Alternatively, you could also listen to the input
event:
Example Here
document.getElementById("keyword").addEventListener('input', function (e) {
document.getElementById("query").textContent = e.target.value;
});
The issue is because the input text value, not change until you release the key.
by using setTimeout
you let the browser to update the value, before running your function (func
)
I recommend not to change onkeyup
, becuase you want onkeydown
, this solution will still work using the onkeydown
event
keyword.onkeydown = function (e) {
setTimeout(func,0)
}
Update Example jsFiddle: http://jsfiddle.net/285cz0np/1/
I recommend you to change to input
event. For example, if the user paste from the clipboard (by using only the mouse), your event will not fire.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With