Best way to track onchange as-you-type in input type=“text”?

后端 未结 16 1148
天命终不由人
天命终不由人 2020-11-22 08:42

In my experience, input type=\"text\" onchange event usually occurs only after you leave (blur) the control.

Is there a way to

相关标签:
16条回答
  • 2020-11-22 09:02

    "input" worked for me.

    var searchBar  = document.getElementById("searchBar");
    searchBar.addEventListener("input", PredictSearch, false);
    
    0 讨论(0)
  • 2020-11-22 09:04

    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()">
    
    0 讨论(0)
  • 2020-11-22 09:08

    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();">
    
    0 讨论(0)
  • 2020-11-22 09:12

    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!!!

    0 讨论(0)
提交回复
热议问题