10

I need to find a way to prevent users from selecting text in a textarea. The goal is to create a custom keyboard that is used to enter text in the textarea. I want users to be able to click on the textarea to set the caret position (which is already working right now). However, I don't want the users to be able to select certain parts of the inserted text, or there should at least be no visual indication of this. I have already tried a few things, such as the following CSS, but without success.

textarea {
    -moz-user-select: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    user-select: none;
}

The solution can be in CSS and/or JavaScript, and it only has to work in Google Chrome.


Disabling the textarea will not work for me. As I mentioned, I want users to be able to place the caret at certain positions by clicking on the location. If I would disable the textarea, this functionality would be lost.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Bram W.
  • 1,587
  • 4
  • 16
  • 39
  • Unless I'm reading this wrong, wouldn't disabled do what you want? – David Nguyen Oct 02 '13 at 14:01
  • http://stackoverflow.com/questions/4712030/disable-text-selection-and-add-exceptions – HIRA THAKUR Oct 02 '13 at 14:02
  • Disabling the text selection is easy... you're really asking how to disable text selection while still allowing users to click to place the cursor at a specific point? – andi Oct 02 '13 at 14:05
  • Disabling the element will also prevent any keyboard actions from getting through, so that is probably not a good solution. – Pointy Oct 02 '13 at 14:06
  • @BramW. well the answer by OPUS *almost* works; it's possible to navigate through the textarea content with arrow keys, but it's not possible to click in the content. Disabling selection I suppose also disables that (which seems wrong, but what can you do). – Pointy Oct 02 '13 at 15:19

8 Answers8

8

A combination of the "readonly" property and "user-select" property works.

The 2nd rule disables the highlighted border on select, which is a kind of visual selection.

The "unselectable" property seems to be Opera/IE specific.

the styles:

.noselect {
   cursor: default;
   -webkit-user-select: none;
   -webkit-touch-callout: none;
   -khtml-user-select: none;
   -moz-user-select: none;
   -ms-user-select: none;
   -o-user-select: none;
}

.noselect:focus {
   outline: none;
}

the markup:

<textarea class="noselect" readonly="readonly" unselectable="on"></textarea>

read-only might be more suitable than disabled (one can still toggle with JS, upon click event).

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • @Gorky for reference: https://developers.google.com/web/updates/tags/removals ...if something had changed, it should be listed there (I'm in a hurry right now, no time to dig further). – Martin Zeitler Jan 21 '19 at 07:40
  • https://bugs.chromium.org/p/chromium/issues/detail?id=881308&can=1&q=user-select%3A%20none%20input&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified Looks like intentional for interop – Gorky Jan 22 '19 at 05:00
4
<textarea unselectable="on" id="my_textarea"></textarea>

    *.unselectable {
   -webkit-user-select: none;
    }  
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
1

Below code can help you

JS Code:

$(document).ready(function(){
   $('#txtInput').bind("cut copy paste",function(e) {
      e.preventDefault();
   });
});
Vinutha N
  • 156
  • 6
1

None of these solutions worked for me. Here's one of my own:

textarea::selection {
    background-color: transparent;
}
E_net4
  • 27,810
  • 13
  • 101
  • 139
Calculamatrise
  • 364
  • 3
  • 6
0

Or you can just put disabled="disabled" in textarea, like this :

<textarea disabled="disabled"></textarea>

silvadori
  • 49
  • 1
  • 4
  • I tried just putting as w3schools and other suggest. This answer was the only way I was able to get it to work. – iampre409 Oct 19 '20 at 13:38
0

The answers didn't work for me, so I put a div with position: absolute; on top of the textarea, and it works perfectly.

Samuel Gfeller
  • 840
  • 9
  • 19
-2

You can disable it with javascript:

document.getElementById("textarea").disable='true';
Mike H.
  • 1,731
  • 9
  • 31
-3
<textarea disabled="readonly"></textarea>

tested it is working.

Ankur Dhanuka
  • 1,217
  • 2
  • 11
  • 22