4
I have a input
and I want it to allow real-time capital letters only.
This code apparently works perfectly.
$("#texto").on("input", function(){
$(this).val($(this).val().toUpperCase());
});
But I noticed a bug and I don’t know how to fix it:
When I have a text for example "RIO JANEIRO" and I want to correct it for "RIO DE JANEIRO", when I type any letter in the middle of the word the input cursor is reset and goes to the last character and then the text is like "RIO D JANEIROE". Note that the first letter is in the correct place and then the cursor is moved to the end.
How can I fix this?
$("#texto").on("input", function(){
$(this).val($(this).val().toUpperCase());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="texto">
the "problem" is the event you are using; when converting the characters to
uppercase
the cursor goes to the end. You could change the event to thefocusout
, for example. Yes, it would be a gambiarra :P– rLinhares