Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make editable textarea text non-selectable

I'm trying to create a textarea that is not read-only (users can type), but they cannot select and drag.

All that I found either turns the textarea into readonly, or disables my ability to focus.

Appreciate any help.

like image 634
Adam Avatar asked Jul 18 '13 17:07

Adam


People also ask

How do I make a textarea non editable?

The readonly attribute is a boolean attribute. When present, it specifies that a text area should be read-only. In a read-only text area, the content cannot be changed, but a user can tab to it, highlight it and copy content from it.

How do I make a text area read only?

The <textarea> readonly attribute in HTML is used to specify that the textarea element is read-only. If the textarea is readonly, then it's content cannot be changed but can be copied and highlighted. It is a boolean attribute.

How do I select non selectable text?

Place the cursor near the text you need to copy. Then press the Windows key + Q and drag the cursor. You should see a blue box that you can now highlight the text by dragging the cursor.


2 Answers

In jQuery 1.8, this can be done as follows:

$('textarea')
    .attr('unselectable', 'on')
    .css('-webkit-user-select', 'none')
    .css('-moz-user-select', 'none')
    .css("-ms-user-select","none")
    .css("-o-user-select","none")
    .css("user-select",'none')
    .on('selectstart', false)
    .on('mousedown', false);

or by just using css,

#yourtextarea
{
  -moz-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
like image 105
Optimus Prime Avatar answered Sep 19 '22 01:09

Optimus Prime


You can use the CSS style user-select: none; to keep text from being selectable.

like image 45
DevlshOne Avatar answered Sep 20 '22 01:09

DevlshOne