Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto select text in HTML input

Tags:

html

input

I have an HTML input as follows:

<input type="text" value="Link etc..." readOnly={true}/>

I am trying to make the text inside the input to be auto highlighted at the point the input is displayed on the page.

I have found lots of questions that show how to do this onClick etc, but none that highlight the text by default when the input displays.

I am sure this is a simple task - but I cannot find the answer anywhere!!

NOTE: I am sure I could work out how to achieve this by firing a JavaScript function on my page - but this seems a bit of overkill - I am trying to achieve this in the HTML declaration

I am also using React - but I do not think this is relevant for this question?

like image 265
Alex Avatar asked Aug 21 '17 11:08

Alex


People also ask

How do you auto select a field in HTML?

To auto select an input field and the text in it on page load with JavaScript, we can use the focus and select methods. to add an input. const input = document. getElementById("myTextInput"); input.

How do you select text in HTML?

The select() method is used to select the content of a text field.

How do you auto fill a textbox in HTML?

The autocomplete attribute specifies whether or not an input field should have autocomplete enabled. Autocomplete allows the browser to predict the value. When a user starts to type in a field, the browser should display options to fill in the field, based on earlier typed values.

How do you select all text in a input?

select() method selects all the text in a <textarea> element or in an <input> element that includes a text field.


1 Answers

AFAIK there is no default feature which highlight an input text when it's active in HTML. You will need to use some Javascript.

I can recommand you to check out this StackOverflow question which provides a simple and pretty efficient code:

<input type="text" name="textbox" value="Test" onclick="this.select()" />

N.B: In a UX point of view, highlight by default an input text on click can be a bad idea. If the user typed something and wants to modify his input, it will hightlight and he will need tu use the keyboard arrows to change the cursor position. Be carefull with this feature.

like image 60
Louis 'LYRO' Dupont Avatar answered Sep 24 '22 23:09

Louis 'LYRO' Dupont