Disable text selection in textarea
Asked Answered
H

8

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.

Handcart answered 2/10, 2013 at 13:56 Comment(5)
Unless I'm reading this wrong, wouldn't disabled do what you want?Junia
#4712530Subservience
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?Unipolar
Disabling the element will also prevent any keyboard actions from getting through, so that is probably not a good solution.Thomasenathomasin
@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).Thomasenathomasin
U
9

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

Uxoricide answered 15/6, 2014 at 20:22 Comment(2)
@Gorky for reference: 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).Uxoricide
bugs.chromium.org/p/chromium/issues/… Looks like intentional for interopCoumarin
S
4
<textarea unselectable="on" id="my_textarea"></textarea>

    *.unselectable {
   -webkit-user-select: none;
    }  
Subservience answered 2/10, 2013 at 13:59 Comment(3)
Has no effect in Firefox.Thomasenathomasin
thats because user wants it only in chrome.We can add moz-user-select:noneSubservience
oops sorry, I missed that, and you're also right that -moz-user-select: none works! I think this is the right answer.Thomasenathomasin
B
1

Below code can help you

JS Code:

$(document).ready(function(){
   $('#txtInput').bind("cut copy paste",function(e) {
      e.preventDefault();
   });
});
Bouse answered 26/4, 2017 at 10:29 Comment(0)
E
1

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

textarea::selection {
    background-color: transparent;
}
Eld answered 11/11, 2021 at 5:50 Comment(2)
this is not correct solution what if someone copy text without knowing he selected the textGiro
My purpose was only to hide the highlight. If you want to disable the text selection, user-select: none; works for me.Eld
O
0

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

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

Overmeasure answered 19/1, 2015 at 12:23 Comment(1)
I tried just putting <textarea disabled></textarea> as w3schools and other suggest. This answer was the only way I was able to get it to work.Penthouse
G
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.

Gaidano answered 24/11, 2022 at 8:59 Comment(0)
S
-2

You can disable it with javascript:

document.getElementById("textarea").disable='true';
Sexlimited answered 2/10, 2013 at 14:0 Comment(0)
M
-3
<textarea disabled="readonly"></textarea>

tested it is working.

Mireyamiriam answered 2/10, 2013 at 14:1 Comment(3)
The OP does not want to make the textarea read-only, he would just want the select functionality to be disabled.Megalo
by disabling the textarea, text selection can be doneMireyamiriam
@AnkurDhanuka disabling the element will prevent keyboard events from being generated, so that probably won't work for the OP.Thomasenathomasin

© 2022 - 2024 — McMap. All rights reserved.