133

How can I make it so when you click inside a textarea, its entire content gets selected?

And eventually when you click again, to deselect it.

Alex
  • 66,732
  • 177
  • 439
  • 641
  • possible duplicate of [JQuery: Selecting Text in an Element (akin to highlighting with your mouse)](http://stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse) – Blender Apr 26 '11 at 23:17
  • 5
    @Blender: No, that question concerns highlighting text in an element, not a textarea. The two are quite different. – Tim Down Apr 26 '11 at 23:26

6 Answers6

199

To stop the user from getting annoyed when the whole text gets selected every time they try to move the caret using their mouse, you should do this using the focus event, not the click event. The following will do the job and works around a problem in Chrome that prevents the simplest version (i.e. just calling the textarea's select() method in a focus event handler) from working.

jsFiddle: http://jsfiddle.net/NM62A/

Code:

<textarea id="foo">Some text</textarea>

<script type="text/javascript">
    var textBox = document.getElementById("foo");
    textBox.onfocus = function() {
        textBox.select();

        // Work around Chrome's little problem
        textBox.onmouseup = function() {
            // Prevent further mouseup intervention
            textBox.onmouseup = null;
            return false;
        };
    };
</script>

jQuery version:

$("#foo").focus(function() {
    var $this = $(this);
    $this.select();

    // Work around Chrome's little problem
    $this.mouseup(function() {
        // Prevent further mouseup intervention
        $this.unbind("mouseup");
        return false;
    });
});
Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • 10
    I think it's better to implement this stuff using a separate "select all text" button since automatically selecting the text on focus or click events is realy annoying. – RobG Apr 27 '11 at 02:01
  • 2
    this fails for me in Chrome, working solution is: http://stackoverflow.com/a/6201757/126600 – zack Sep 05 '12 at 11:11
  • @zack: The jsFiddle example in this answer works for me in Chrome. Does it not for you? I agree the answer you linked to is more foolproof. – Tim Down Sep 07 '12 at 13:45
  • @TimDown: you are right, I was being a bit over zealous - actually it does work correctly on 'click', but fails if you `tab` into the textarea - the your other solution works for both cases :) – zack Sep 07 '12 at 18:16
  • Change the above code slightly and it will work like a charm.. `$("#foo").mouseup(function() {` `$("#foo").unbind("mouseup");` `return false;` `});` you need to refer the textbox without using `this` just refer it with full path.. and it will work.. – pratikabu Nov 26 '12 at 12:01
  • @TimDown it was not working for me also.. but after replacing it with absolute reference it worked fine.. i'm using jQuery 1.8.. – pratikabu Dec 01 '12 at 18:28
15

Better way, with solution to tab and chrome problem and new jquery way

$("#element").on("focus keyup", function(e){

        var keycode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
        if(keycode === 9 || !keycode){
            // Hacemos select
            var $this = $(this);
            $this.select();

            // Para Chrome's que da problema
            $this.on("mouseup", function() {
                // Unbindeamos el mouseup
                $this.off("mouseup");
                return false;
            });
        }
    });
Matiesky
  • 151
  • 1
  • 2
11

I ended up using this:

$('.selectAll').toggle(function() {
  $(this).select();
}, function() {
  $(this).unselect();
});
Alex
  • 66,732
  • 177
  • 439
  • 641
  • but I don't know how to check if the text is already selected, so I can reverse the two actions :( – Alex Apr 26 '11 at 23:35
  • 1
    @Alex: I wouldn't mess too much with this if I were you. Users expect standard behaviour from textareas. – Tim Down Apr 26 '11 at 23:45
  • no, this particular textarea is only meant for copy-paste. all the text I have inside it is a big encryted string which can only be either entirely replaced, or copied to the clipboard – Alex Apr 26 '11 at 23:48
  • @Alex: Ah, right. You might want to make it read-only by adding the `readonly` attribute then. – Tim Down Apr 26 '11 at 23:55
  • Consider making the text content of a div. – RobG Apr 27 '11 at 02:04
  • @RobG: If it was a div, copy/paste would require more code (and likely a flash-based plugin), rather than using the built-in clipboard capabilities of a textarea. – Hollister Jul 03 '11 at 00:44
  • 1
    @Hollister: No, it's perfectly possible for either user or script to select content within a div. You're probably thinking of copying to the clipboard, which isn't possible in script without a Flash-based library like ZeroClipboard. – Tim Down Sep 13 '11 at 09:05
  • toggle() is now deprecated AND removed – realtebo Jun 04 '13 at 11:29
7
$('textarea').focus(function() {
    this.select();
}).mouseup(function() {
    return false;
});
Phil LaNasa
  • 2,907
  • 1
  • 25
  • 16
5

Slightly shorter jQuery version:

$('your-element').focus(function(e) {
  e.target.select();
  jQuery(e.target).one('mouseup', function(e) {
    e.preventDefault();
  });
});

It handles the Chrome corner case correctly. See http://jsfiddle.net/Ztyx/XMkwm/ for an example.

Ztyx
  • 14,100
  • 15
  • 78
  • 114
  • Hi, I was tested your code and it's working. Currently, in my situation I am using `disabled` attribute in my ``, but it's not working, so how to fix it? – mastersuse Nov 22 '21 at 07:17
4

Selecting text in an element (akin to highlighting with your mouse)

:)

Using the accepted answer on that post, you can call the function like this:

$(function() {
  $('#textareaId').click(function() {
    SelectText('#textareaId');
  });
});
Community
  • 1
  • 1
Todd
  • 676
  • 5
  • 12
  • Maybe flagging this question as a duplicate might be more useful? It wasn't really your answer, so it'd be better to merge the two questions. – Blender Apr 26 '11 at 23:17
  • Agreed -- Though the OP could benefit from the added explanation for her implementation. :) – Todd Apr 26 '11 at 23:18
  • That question concerns highlighting text in an element, not a textarea. The two are quite different. – Tim Down Apr 26 '11 at 23:26
  • thanks, I found out that I can do this with `$(this).select()`, I'll use that because it's less code :) – Alex Apr 26 '11 at 23:34