Instead of using onKeyPress
, I recommend that you use onKeyUp
, it works, when you stop pressing the key.
So the only thing we do is, using the function toUpperCase()
we will convert the whole chain in general, and give the effect that is done one by one, since in each event of onKeyUp()
the function will be executed.
In other words, it is not necessary to convert capital letters letter by letter.
Likewise, you can replicate the function in other <input type='text'>
and it will work the same, so there is no problem in that it is general.
function mayus(e) {
e.value = e.value.toUpperCase();
}
<input type="text" onkeyup="mayus(this);">
<input type="text" onkeyup="mayus(this);">
Another way to solve it would be like this:
When creating the <input type='text'>
, you assign a id
to each, and later you can apply this formula.
<input type="text" id="campo" onKeyUp="document.getElementById(this.id).value=document.getElementById(this.id).value.toUpperCase()">