In my experience, input type=\"text\"
onchange
event usually occurs only after you leave (blur
) the control.
Is there a way to
"input" worked for me.
var searchBar = document.getElementById("searchBar");
searchBar.addEventListener("input", PredictSearch, false);
I think in 2018 it's better to use the input
event.
-
As the WHATWG Spec describes (https://html.spec.whatwg.org/multipage/indices.html#event-input-input):
Fired at controls when the user changes the value (see also the change event)
-
Here's an example of how to use it:
<input type="text" oninput="handleValueChange()">
Method 1: Add an event listener for input
:
element.addEventListener("input", myFunction);
Method 2: Define the oninput
property with JavaScript:
element.oninput = function()
{
myFunction();
};
Method 3: Define the oninput
property with HTML:
<input type="text" oninput="myFunction();">
there is a quite near solution (do not fix all Paste ways) but most of them:
It works for inputs as well as for textareas:
<input type="text" ... >
<textarea ... >...</textarea>
Do like this:
<input type="text" ... onkeyup="JavaScript: ControlChanges()" onmouseup="JavaScript: ControlChanges()" >
<textarea ... onkeyup="JavaScript: ControlChanges()" onmouseup="JavaScript: ControlChanges()" >...</textarea>
As i said, not all ways to Paste fire an event on all browsers... worst some do not fire any event at all, but Timers are horrible to be used for such.
But most of Paste ways are done with keyboard and/or mouse, so normally an onkeyup or onmouseup are fired after a paste, also onkeyup is fired when typing on keyboard.
Ensure yor check code does not take much time... otherwise user get a poor impresion.
Yes, the trick is to fire on key and on mouse... but beware both can be fired, so take in mind such!!!