60

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?

Carl
  • 601
  • 1
  • 5
  • 3

8 Answers8

65

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

Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • 9
    onclick="this.focus();this.select()" just works fine in Chrome also – mars-o Aug 09 '13 at 22:21
  • @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
    On Chrome 28, `onclick="this.select()"` does suffice and works! – Marco Demaio Dec 06 '13 at 17:13
  • 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
  • @A.Genedy: I agree. The question only mentioned clicking though. – Tim Down Apr 23 '18 at 10:54
  • 2022 here: This works with Chrome for some time now. – Jan Feldmann Jun 29 '22 at 10:08
  • Down-voted for mentioning jQuery on a JavaScript question. – John Nov 22 '22 at 03:27
  • @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
32
onclick="this.focus();this.select()" readonly="readonly"
maťo
  • 1,225
  • 8
  • 16
16
<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

GraphicsMuncher
  • 4,583
  • 4
  • 35
  • 50
Julia
  • 171
  • 1
  • 5
  • 2
    document.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
5

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()

fireshadow52
  • 6,298
  • 2
  • 30
  • 46
3

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>
  • 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
2

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')"
Ignacio Ara
  • 2,476
  • 2
  • 26
  • 37
2

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

Bassem
  • 3,135
  • 2
  • 25
  • 41
1
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>
Carson
  • 6,105
  • 2
  • 37
  • 45