Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

div contentEditable but Readonly

I have a div with some text and I want when the cursor is hover this div to select the text. If I let this div as it is, when trying to select all (CTRL+A) then I select all page content, meaning all body text.

To get rid of this, I need to use contenteditable attribute for this div.

But I don't want to let people to change the text / copy / cut and so on

I try to use readonly for this div, but doesn't working.

Any advice please ?

PS1: This div has also other tags inside (html content), but I don't think that this is a problem.

PS2: An example is here: jsfiddle.net/msakamoto_sf/wfae8hzv/ - but with a problem. You can cut the text :(

like image 312
Simona Avatar asked May 16 '15 08:05

Simona


People also ask

How do I make a div non editable?

style. display = 'inline-block'; will do it.

What is Contenteditable div?

Definition and Usage The contenteditable attribute specifies whether the content of an element is editable or not.

How do I make a div content editable?

Answer: Use the HTML5 contenteditable Attribute You can set the HTML5 contenteditable attribute with the value true (i.e. contentEditable="true" ) to make an element editable in HTML, such as <div> or <p> element.

How do I make a button non editable in HTML?

The Boolean readonly attribute, when present, makes the element not mutable, meaning the user can not edit the control.


2 Answers

Use event.metaKey in the keydown event to check if ctrl (or cmd on mac) keys are being pressed. You also have to disable the cut and paste events.

<div
  contenteditable="true"
  oncut="return false"
  onpaste="return false"
  onkeydown="if(event.metaKey) return true; return false;">
    content goes here
</div>
like image 170
drdator Avatar answered Sep 17 '22 15:09

drdator


set contenteditable to false and it should work !! that simple.

use contenteditable attribute for div to make it editable or not and use readonly attr for form input elements.

<element contenteditable="true|false">

<input readonly="readonly" />
like image 33
Dinnu Buddy Avatar answered Sep 17 '22 15:09

Dinnu Buddy