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?

- 601
- 1
- 5
- 3
-
[2020] modern Browsers have ``setSelectionRange`` https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange – Danny '365CSI' Engelman Jul 19 '20 at 09:21
8 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

- 318,141
- 75
- 454
- 536
-
9
-
@mars-o: So it does. I wonder if Chrome changed its behaviour or whether I was always wrong about that. – Tim Down Aug 10 '13 at 10:59
-
2
-
1`onfocus="this.select()"` works better when using the TAB key, and also allows users to easily control where they want to place the caret. – A. Genedy Apr 22 '18 at 10:26
-
-
-
-
@John: Heh. I was in 2011, and remain, the last person to force jQuery solutions to non-jQuery questions on Stack Overflow. If you'd looked at the linked answer, you'd see that it provides both a non-jQuery answer (which is relevant to this question) and a jQuery version. Also, please don't "correct" my spelling to American English. – Tim Down Nov 22 '22 at 12:33
<script type="text/javascript">
function SelectAll(id)
{
document.getElementById(id).focus();
document.getElementById(id).select();
}
</script>
Textarea:<br>
<textarea rows="3" id="txtarea" onClick="SelectAll('txtarea');" style="width:200px" >This text you can select all by clicking here </textarea>
I got this code here

- 4,583
- 4
- 35
- 50

- 171
- 1
- 5
-
2document.getElementById(id) could be omitted, just pass `this` as a parameter instead: `function SelectAll(el) { el.focus(); el.select(); };` – Nik Sumeiko Jan 14 '14 at 10:19
onclick="this.focus()"
is redundant, as the focus()
method is the same as clicking in the textarea (but it places the cursor at the end of the text).
highlight()
isn't even a function, unless of course you created it somewhere else.
Conclusion: do this.select()

- 6,298
- 2
- 30
- 46
Seem to more browsers supporting setSelectionRange()
than select()
1 way: - Use setSelectionRange()
https://caniuse.com/#search=setSelectionRange
const my_textarea = document.getElementById("my_textarea");
document.getElementById("my_but").onclick = function () {
if(my_textarea.value !== ""){
my_textarea.onfocus = function () {
my_textarea.setSelectionRange(0, my_textarea.value.length);
my_textarea.onfocus = undefined;
}
my_textarea.focus();
}
}
<textarea id="my_textarea" name="text">1234567</textarea>
<br>
<button id="my_but">Select</button>
2 way: - Use select()
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select#browser_compatibility
const my_textarea = document.getElementById("my_textarea");
document.getElementById("my_but").onclick = function () {
if(my_textarea.value !== ""){
my_textarea.onfocus = function () {
my_textarea.select();
my_textarea.onfocus = undefined;
}
my_textarea.focus();
}
}
<textarea id="my_textarea" name="text">1234567</textarea>
<br>
<button id="my_but">Select</button>

- 1,622
- 2
- 23
- 27
-
The .select() method is the most supported: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select#browser_compatibility. Your second link to caniuse doesn't show the `.select()` method. – thibpat Sep 27 '22 at 14:15
To complete other answers perhaps you would like to copy the code/text you've just clicked, so use:
onclick="this.focus();this.select();document.execCommand('copy')"

- 2,476
- 2
- 26
- 37
You have to use the .focus() as well as the .select() Javascript function to achieve the desired result.
Check the link below for an example:
http://www.plus2net.com/javascript_tutorial/textarea-onclick.php

- 3,135
- 2
- 25
- 41
const elem = document.elementFromPoint(clientX, clientY)
if (elem.select) { // Make sure the method exists.
elem.focus()
elem.select()
}
You may not want to spend time finding your object.
For example, you have written extensions to inject scripts into the web page.
At this time, you do not need to consider the element ID that you can apply immediately.
Example
<textarea rows="3" style="width:200px">"Double click" or Press "F4" to select all text</textarea>
<script>
let clientX, clientY
document.addEventListener("mousemove", (e) => {
clientX = e.clientX
clientY = e.clientY
})
selectAllFunc = () => {
const elem = document.elementFromPoint(clientX, clientY)
if (elem.select) { // Make sure the method exists.
elem.focus()
elem.select()
}
}
document.addEventListener("keydown", (keyboardEvent) => {
if (keyboardEvent.key === "F4") { // && keyboardEvent.ctrlKey
selectAllFunc()
}
})
document.addEventListener("dblclick", (e) => {
selectAllFunc()
})
</script>

- 6,105
- 2
- 37
- 45