$('.myClass').on('input', function() {
$(this).val($(this).val().toUpperCase());
});
perform toUpperCase() each time the content of an input field is changed
-
08-07-2023 - |
Question
I want to perform toUpperCase() each time the content of an input field is changed. I mean not when the field lost focus, but each time a character is typed.
I try this which doesn't work (I'm fairly new to JQ)!
$(".myClass").change(function () {
$(this).val().toUpperCase();
});
Besides the function is launched only on blur.
What's wrong?
Solution
OTHER TIPS
Function will fire change
event but You need to reset that value using .val()
as just setting the text to toUpperCase()
isn't enough.
$(".myClass").change(function () {
$(this).val($(this).val().toUpperCase());
});
To limit the characters in the browser input to upper case, use css-
someinput{text-transform:uppercase}
Then use any change event or a validation before submitting to change the value you are sending to the server.
You should use another keyboard event:
https://api.jquery.com/keypress/
$( "#test" ).keypress(function(e) {
$(this).val($(this).val().toUpperCase());
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow