Position the cursor

Asked

Viewed 893 times

3

I have a function with Jquery that leaves the text of 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 goes back to the end, this only happens in fields with mask, follows 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);
    });
  }
});

1 answer

3


I suggest you use the function change() jquery. It is called when something is typed or pasted or etc. So you don’t need timeouts and you don’t need two functions to do the same thing.https://api.jquery.com/change/

  • Thanks worked out, as change() runs after the element lose focus I have no problem with the cursor in the fields with mask.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.