progster
progster

Reputation: 89

Text input event handler

I have a text input and a handler routine testFunc(input.value)

testFunc should be called when

  1. When a alphabet key is pressed
  2. when backspace is hit with updated value (i.e when initial input is 1234, when backspace is hit from the end, input should be 123 to the testFunc routine)
  3. If the input is 1234, and backspace is hit from somewhere in the middle i.e 1234, then the testfunc should be called as testFunc(234).
  4. 2, 3 should be similar for DELETE key too.

I tried with onkeypress and onkeyup, but some how testFunc handler routine is not getting with the updated value (or) testFunc routine itself is not getting called.

Can any one let me know how i can resolve this issue or whether there is any existing APIs in Javascript

Upvotes: 0

Views: 8545

Answers (2)

Tim Down
Tim Down

Reputation: 324567

You can use the HTML5 input event, which is fired whenever the input value changes, and fires after the value has changed. In combination with an IE workaround, it's surprisingly compatible. See the following answer:

jQuery keyboard events

Upvotes: 3

RobG
RobG

Reputation: 147373

In which browsers? The following always shows the current value of the input whenever a key is pressed:

  <script type="text/javascript">

  function show(s) {
    document.getElementById('msg').innerHTML = s;
  }

  </script>

  <input type="text" onkeyup="show(this.value);">

  <div id="msg"></div>

Upvotes: 0

Related Questions