0

Consider the following:

<input type="text" id="foo" />

I need that, when the value of the input.#foo is changed, without blur, it shot my event. Here comes the problem: I can't use onkey events because the input is dinamically received by a virtual HTML-composed keyboard.

I can easily do, with the true keyboard:

document.querySelector("#foo").addEventListener('keypress', function() {
  // Implementation
});

The W3C recommend using onchange, but onchange only works after the blur event occurs. I need a mix of onkeypress, to be dynamic, on time modify, and onchange, to know when it changes. You can see this: http://jsfiddle.net/zuq733La/

Marcelo Camargo
  • 2,240
  • 2
  • 22
  • 51

2 Answers2

0

the only way I can think of (without using blur, change, keypress, keyup or keydown events) is using a setInterval(): DEMO

var value=$('#foo').val();
setInterval(function(){
    if(value!=$('#foo').val()){
        value=$('#foo').val();
        //here you need to write what will happen if the value is changed
    }
},50);

in the DEMO you can see that even if you copy and paste a text (using only your mouse even without blurring the input) the event fires.

Amin Jafari
  • 7,157
  • 2
  • 18
  • 43
0

JS Fiddle

use textInput it will check for every character added

document.querySelector("#foo").addEventListener('textInput', function () {
   alert('Value changed!');
});
Vitorino fernandes
  • 15,794
  • 3
  • 20
  • 39