Select All or Highlight Text Fields and Textareas Using Javascript
To select all or highlight text fields and textareas using Javascript:
<textarea onClick=select()>This is some text.</textarea>
<input type=text onClick=select() value="This is text too.">
-We created a textarea and a text field.
-We used Javascript's built-in onClick event handler.
-We used Javascript's built-in select method to highlight the text.
NOTES: Many times this is convenient for allowing users to copy and paste. Keep in mind, the select method prevents the user from editing the text in place. If the user tries to type, they will delete the highlighted text, which might be the desired effect.
If you want to prevent typing, using HTML's readonly like so:
<input type=text onClick=select() value="This is text too." readonly>
Free Copy and Paste Javascript Code:
<form>
<textarea onClick=select() readonly>This is some text.</textarea>
<input type=text onClick=select() value="This is text too." readonly>
</form>