I would like that when typing the keys they would be converted to Uppercase, taking into account that it can be done with any input of the form, that is, that the formula is general.
I have the following:
function mayus(e) {
var tecla=e.value;
var tecla2=tecla.toUpperCase();
alert(tecla2);
}
<input type="text" onkeypress="mayus(this);">
Instead of using
onKeyPress
, I recommend that you useonKeyUp
, it acts, when you stop pressing the key.So the only thing we do is, using the function we
toUpperCase()
will convert the whole string in general, and it will give the effect that it is done one by one, since in each event ofonKeyUp()
the function will be executed.That is, it is not necessary to convert to uppercase letter by letter.
Still, you can replicate the function in others
<input type='text'>
and it will work just the same, so there is no problem in making it general.Another way to solve it would be like this:
When creating the
<input type='text'>
, you assign oneid
to each one, and later you can apply this formula.The easiest way without having to call javascript just include this line in your css:
and ready.
With jQuery you can change to Uppercase the text of any
<input>
or<textarea>
, try the following code by inserting it in the head (<head>
) of the document:You can also use CSS to indicate that
<input>
or<textarea>
you want to convert to uppercase:I had the same problem. However I have a development using WebForms in ASP.Net.
To complicate things I am using DevExpress controls. And since their "inputs" (or TextBoxes) are made based on tables, I had to do it this way:
I decided to do it this way because I have that function in a separate javascript document, since I plan to reuse it in other areas of the document and in other documents. In fact, I would recommend following the pattern suggested by the DevExpress controls themselves so that everything is clearer, as in the following:
This way we can notice that we get the reference to the control that invokes the event in its source(s) argument to work with it.
Thank you very much for the previous replies, they were very helpful.
Greetings to all.
I use this (reading the event
keyup
) and it works for me without problems: