Position cursor

3

I have a function with JQuery that leaves the textbox in uppercase, the problem is that when I have a mask in the field, after typing any character, the cursor goes to the end of the textbox, then the user has to go back and type again, and the cursor returns to the end, this only happens in fields with masks, follow the function.

jQuery(document).ready(function ($) {
// Chamada da funcao upperText(); ao carregar a pagina
upperText();
// Funcao que faz o texto ficar em uppercase
function upperText() {
    // Para tratar o colar
    $(":input").bind('paste', function (e) {
        var el = $(this);
        setTimeout(function () {
            var text = $(el).val();
            el.val(text.toUpperCase());
        }, 100);
    });

    // Para tratar quando é digitado
    $(":input").keypress(function () {
        var el = $(this);
        setTimeout(function () {
            var text = $(el).val();
            el.val(text.toUpperCase());
        }, 100);
    });
  }
});
    
asked by anonymous 04.05.2015 / 22:37

1 answer

3

I suggest you use the change() function of JQuery. It is called when something is typed or pasted or so on. So you do not need timeouts and do not need two functions to do the same thing. link

    
05.05.2015 / 10:43