admin管理员组文章数量:1327487
How can i retrieve the contents of an input field as they are entered with JavaScript?
i know .onChange only works after the focus is changed from the input field?
thanks
How can i retrieve the contents of an input field as they are entered with JavaScript?
i know .onChange only works after the focus is changed from the input field?
thanks
Share Improve this question asked Sep 8, 2010 at 12:01 Mo.Mo. 42.6k38 gold badges90 silver badges134 bronze badges 2- onblur event will fire once the focus is out of the textbox control. You can handle it in OntextChanged event – Bala Commented Sep 8, 2010 at 12:23
- 1 @Mo: I published a blog post on this subject today, might be an interesting read for you - whattheheadsaid./2010/09/… – Andy E Commented Sep 22, 2010 at 11:37
4 Answers
Reset to default 10EDIT: since writing this answer, I learned of the HTML5 oninput
event which is much more appropriate than key events because it will detect all forms of input including paste, drag and drop, etc. The event is supported in all major browsers except IE 8 and lower, but you can simulate the event in IE by mapping to onpropertychange
instead. Example:
if ("onpropertychange" in myInput && !("oninput" in myInput)) {
myInput.onpropertychange = function () {
if (event.propertyName == "value")
myHandler.call(this, event);
}
}
else
myInput.oninput = myHandler;
Note that onpropertychange
doesn't fire when inputting into non-form elements with the contentEditable
property set.
onkeyup will only fire after the key is lifted, so for best results use onkeypress and/or onkeydown. If you use a timer with a delay of 0ms, you can get the new value immediately after it is updated:
myInput.onkeydown = function () {
var self = this;
window.setTimeout(function () {
alert(self.value);
},0);
}
Example: http://jsfiddle/fgcYD/
Note that this won't catch pasting or dragging and dropping text into the box. For those you need other events.
You should try the onKeyUp event. Here's a simple example:
<input type="text" onkeyup="document.getElementById('xyz').innerHTML=this.value">
<div id="xyz"></div>
If your input field has focus, use the onKeyUp event to monitor the user typing text.
http://www.w3schools./jsref/event_onkeyup.asp
Once I had used a bination of "onKeyup", "onBlur", onKeyPress to handle all the circumstances while making a TextBox to allow only numeric values. Like this:
//C#
tc.Attributes.Add("onKeyup", "extractNumber(this,-1,true);"); tc.Attributes.Add("onBlur", "extractNumber(this,-1,true,true);"); tc.Attributes.Add("onKeyPress", "return blockNonNumbers(this, event, true, true);");
with this .js code: http://www.mredkj./tutorials/validate2.js
And it still works pretty well.
本文标签: how can i retrieving text from input field as its entered in JavaScriptStack Overflow
版权声明:本文标题:how can i retrieving text from input field as its entered in JavaScript?? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742206276a2432872.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论