I have a text input and a handler routine testFunc(input.value)
testFunc should be called when
- When a alphabet key is pressed
- 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)
- 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).
- 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
- When a alphabet key is pressed
- 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)
- 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).
- 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
2 Answers
Reset to default 3You 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>