How to select all text in HTML text input when clicked?

Spread the love

Sometimes, we want to select all text in HTML text input when clicked.

In this article, we’ll look at how to select all text in HTML text input when clicked.

How to select all text in HTML text input when clicked?

To select all text in HTML text input when clicked, we use the select method.

For instance, we write

<input value="Sample Text" />

to add an input.

Then we write

const textInput = document.querySelector("input");
textInput.onclick = () => {
  textInput.select();
};

to select the input with querySelector.

Then we set its onclick property to a function that calls select to highlight the input value when we click on the input.

Conclusion

To select all text in HTML text input when clicked, we use the select method.

Leave a Reply

Your email address will not be published.