Position cursor at end of text when clicking input

2

Can you make every time I click the input the cursor moves to the end of it?

Notice that in this image it is before 1 and wanted every time you click on the input it goes to the end.

    
asked by anonymous 06.06.2017 / 21:47

2 answers

6

Yes, the simplest way, half "brute force" is to rewrite the input value with itself. This forces the cursor to go to the end.

Try moving the cursor with the arrows and then clicking it.

var input = document.querySelector('input');
input.addEventListener('click', function() {
  this.value = this.value;
});
<input type="text" value="algo para testar" />
    
06.06.2017 / 21:52
1

Another alternative, with Javascript:

<input onfocus="this.selectionStart = this.selectionEnd = 500;" value="Olá texto">
    
06.06.2017 / 21:55