Doubt onkeyup javascript

4

When I type in input some word, the text entered goes to uppercase.

In Mozilla Firefox and Internet Explorer I can return the blinking indicator to type a letter and I can edit it anywhere.

In Google Chrome, every time I return to the bookmark, it automatically goes to the end of the typed text. How do I make it look like the other browsers, where is it possible to go back and not go to the end of the text?

Nome: <input type="text" id="fnome" onkeyup="javascript:this.value=this.value.toUpperCase();">
    
asked by anonymous 28.08.2015 / 01:48

1 answer

5

No Javascript

Try using CSS for this rather than Javascript, so do not rewrite the input value:

Nome: <input type="text" id="fnome" style="text-transform: uppercase;">

See example here .

With Javascript (jQuery)

If you do Javascript question or, for some reason, like to keep the "transformation effect" of the lyrics lowercase to uppercase and walk with the cursor through input without it returning to the end position, you can do this:

HTML:

Nome: <input type="text" id="fnome">

JS:

$("#fnome").keyup(function(){

    var start = this.selectionStart,
        end = this.selectionEnd;

    $(this).val( $(this).val().toUpperCase() );
    this.setSelectionRange(start, end);
});

See example here .

    
28.08.2015 / 01:55