最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Text input event handler - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Aug 31, 2011 at 0:00 Flambino 18.8k2 gold badges43 silver badges58 bronze badges asked Aug 30, 2011 at 22:46 progsterprogster 893 silver badges11 bronze badges 1
  • 1 Please show us your code – rlb.usa Commented Aug 30, 2011 at 22:52
Add a ment  | 

2 Answers 2

Reset to default 3

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

jQuery keyboard events

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>
发布评论

评论列表(0)

  1. 暂无评论