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?
The 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With