admin管理员组文章数量:1245618
I´m trying to program html input onkeypress event from javascript but it doesn´t work, but I can program atributes like size or type.
var element3 = document.createElement("input");
element3.type = "text"
element3.size = "6"
element3.onkeypress= "return isNumberKeyDecimal(event)"
Is that possible?
I´m trying to program html input onkeypress event from javascript but it doesn´t work, but I can program atributes like size or type.
var element3 = document.createElement("input");
element3.type = "text"
element3.size = "6"
element3.onkeypress= "return isNumberKeyDecimal(event)"
Is that possible?
Share Improve this question edited Sep 4, 2018 at 8:32 Mikelon85 asked Oct 22, 2012 at 8:40 Mikelon85Mikelon85 4321 gold badge16 silver badges31 bronze badges4 Answers
Reset to default 8The onkeypress
property accepts a function, not a string.
element3.onkeypress = isNumberKeyDecimal;
But also take a look at the addEventListener
function for the preferred approach to dealing with event listener functions.
In particular, you may wish to look at event delegation, which would allow you to have a single event listener on a container element rather than having to bind it to each input you create.
element3.setAttribute("onkeypress", "return isNumberKeyDecimal(event)");
element3.setAttribute("onkeypress", "return isNumberKeyDecimal(event)");
Mozilla reference here
It should be set as a function not a string.
element3.onkeypress = function(event){return isNumberKeyDecimal(event)}
本文标签: Is it possible to program HTML input onKeyPress event with javaScriptStack Overflow
版权声明:本文标题:Is it possible to program HTML input onKeyPress event with javaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740188970a2238740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论