admin管理员组文章数量:1026639
I have an input
element with an onkeypress
event that runs a function, change()
. The problem is that the function doesn't run for the very first keypress. For example, I have to type 55 instead of 5 to get the function to update, and the value it takes is 5. Somehow, it doesn't recognize the most recent keypress. I tried adding other events, such as onfocus
and onselect
, to no avail. Here is the code for the input
:
<input id="x" type="text" size="1" maxlength="2" onkeypress="change()" onfocus="change()" onselect="change()" onblur="change()" onchange="change()"></input>
I have an input
element with an onkeypress
event that runs a function, change()
. The problem is that the function doesn't run for the very first keypress. For example, I have to type 55 instead of 5 to get the function to update, and the value it takes is 5. Somehow, it doesn't recognize the most recent keypress. I tried adding other events, such as onfocus
and onselect
, to no avail. Here is the code for the input
:
<input id="x" type="text" size="1" maxlength="2" onkeypress="change()" onfocus="change()" onselect="change()" onblur="change()" onchange="change()"></input>
Share
Improve this question
asked May 14, 2013 at 22:12
IanIan
6,1846 gold badges46 silver badges76 bronze badges
2
-
Would using
onkeyup
instead work? Also, what does thechange()
function do and how do you know it is not running it the first time? – Nope Commented May 14, 2013 at 22:15 - Yes! Thanks, I had no idea that existed. Please post it as an answer so I can select it. – Ian Commented May 14, 2013 at 22:19
3 Answers
Reset to default 2I think the issue might be that the onkeypress
event triggers before the value is inserted.
If you want to capture the value after the key has been pressed you could use the onkeyup
event instead and see if that works better.
you might to try this event
onKeyUp="change()"
link no longer valid
I guess it is because the value is just set after you release the key. When I try it with onkeyup
its working
http://jsfiddle/j8B8b/2/
I have an input
element with an onkeypress
event that runs a function, change()
. The problem is that the function doesn't run for the very first keypress. For example, I have to type 55 instead of 5 to get the function to update, and the value it takes is 5. Somehow, it doesn't recognize the most recent keypress. I tried adding other events, such as onfocus
and onselect
, to no avail. Here is the code for the input
:
<input id="x" type="text" size="1" maxlength="2" onkeypress="change()" onfocus="change()" onselect="change()" onblur="change()" onchange="change()"></input>
I have an input
element with an onkeypress
event that runs a function, change()
. The problem is that the function doesn't run for the very first keypress. For example, I have to type 55 instead of 5 to get the function to update, and the value it takes is 5. Somehow, it doesn't recognize the most recent keypress. I tried adding other events, such as onfocus
and onselect
, to no avail. Here is the code for the input
:
<input id="x" type="text" size="1" maxlength="2" onkeypress="change()" onfocus="change()" onselect="change()" onblur="change()" onchange="change()"></input>
Share
Improve this question
asked May 14, 2013 at 22:12
IanIan
6,1846 gold badges46 silver badges76 bronze badges
2
-
Would using
onkeyup
instead work? Also, what does thechange()
function do and how do you know it is not running it the first time? – Nope Commented May 14, 2013 at 22:15 - Yes! Thanks, I had no idea that existed. Please post it as an answer so I can select it. – Ian Commented May 14, 2013 at 22:19
3 Answers
Reset to default 2I think the issue might be that the onkeypress
event triggers before the value is inserted.
If you want to capture the value after the key has been pressed you could use the onkeyup
event instead and see if that works better.
you might to try this event
onKeyUp="change()"
link no longer valid
I guess it is because the value is just set after you release the key. When I try it with onkeyup
its working
http://jsfiddle/j8B8b/2/
本文标签: javascriptRun a function at the first keypress event on an inputStack Overflow
版权声明:本文标题:javascript - Run a function at the first keypress event on an input - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745650513a2161290.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论