Position cursor at the end of text by clicking input

Asked

Viewed 1,683 times

2

It has to do with every time I click on the input the cursor goes to the end of it?

inserir a descrição da imagem aqui

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

  • This question may help you: https://stackoverflow.com/questions/511088/use-javascript-to-place-cursor-at-end-of-text-in-text-input-element

  • The above companion alternatives do not work in some browsers, if you have using pure js you can do a function that is executed in input onfocus

2 answers

6


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

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

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

1

Another alternative, with Javascript:

<input onfocus="this.selectionStart = this.selectionEnd = 500;" value="Olá texto">

  • It didn’t work for me.

  • Use Chrome @jbueno? Use safari and firefox, is what I had tested, after you spoke I saw that Chrome really does not work. I will investigate why.

  • 1

    Yes, I use Chrome. I also found it very strange, because it should be the right way to do it. Anyway, I tested it on safari and ff and it worked, so it gets my vote.

Browser other questions tagged

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