Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

highlight all text in textarea

Tags:

javascript

I want to select all of the text inside of a textarea when a user clicks the textarea. I tried onclick="this.focus()", but this didn't do anything. I tried onclick="this.highlight()", but this caused an error. What should I do?

like image 947
Carl Avatar asked Sep 06 '11 00:09

Carl


People also ask

How do I select all text in textarea?

The select() method selects the entire contents of a text area.

How do you highlight text in textarea?

You can't actually highlight text in a <textarea> . Any markup you would add to do so would simply display as plain text within the <textarea> . The good news is, with some carefully crafted CSS and JavaScript, you can fake it.

How do you highlight a text box in HTML?

Highlight using the HTML5 <mark> tag If you are working on an HTML5 page, the <mark> tag can quickly highlight text. Below is an example of the how to use the mark tag and its result. If your browser supports the <mark> tag, "highlighted text" should have a yellow background.


2 Answers

This may annoy your users since it prevents the useful default behaviour of placing the caret where the user clicked and I therefore recommend against it in general. That said, the solution for most browsers is onclick="this.select()".

However, this will not work in Chrome [UPDATE February 2014: it does now seem to work in recent versions of Chrome]. For a workaround and general background on this issue, see the following question: jQuery - select all text from a textarea

like image 124
Tim Down Avatar answered Sep 19 '22 15:09

Tim Down


onclick="this.focus();this.select()" readonly="readonly" 
like image 39
maťo Avatar answered Sep 18 '22 15:09

maťo