1

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?

potashin
  • 44,205
  • 11
  • 83
  • 107
user3553401
  • 109
  • 2
  • 15

4 Answers4

2
$('.myClass').on('input', function() {
     $(this).val($(this).val().toUpperCase());
});
potashin
  • 44,205
  • 11
  • 83
  • 107
0

You should use another keyboard event:

https://api.jquery.com/keypress/

$( "#test" ).keypress(function(e) {
    $(this).val($(this).val().toUpperCase());
});
Jaime García Pérez
  • 953
  • 1
  • 10
  • 16
  • Thanks. Your function is launched on each keystroke, that's fine, but the toUpperCase() doesn't perform! Text remains unchanged. – user3553401 Apr 23 '14 at 15:08
0

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());
});

Fiddle Demo

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
0

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.

kennebec
  • 102,654
  • 32
  • 106
  • 127