Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove formatting from a contentEditable div

I have a contentEditable Div and I want remove any formatting especially for copy and paste text.

like image 578
Garth Humphreys Avatar asked Aug 01 '11 14:08

Garth Humphreys


People also ask

How do I turn off Contenteditable div?

Just set contentEditable="false" . See this answer.

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 stop Enter key in Contenteditable?

To prevent contenteditable element from adding div on pressing enter with Chrome and JavaScript, we can listen for the keydown event on the contenteditable element and prevent the default behavior when Enter is pressed. to add a contenteditable div. document. addEventListener("keydown", (event) => { if (event.

How do you focus on Contenteditable?

Changing your tabIndex to >= 0 will let you focus on the elements.


1 Answers

document.querySelector('div[contenteditable="true"]').addEventListener("paste", function(e) {         e.preventDefault();         var text = e.clipboardData.getData("text/plain");         document.execCommand("insertHTML", false, text);     }); 

It is simple: add a listener to the "paste" event and reeformat clipboard contents.

Here another example for all containers in the body:

[].forEach.call(document.querySelectorAll('div[contenteditable="true"]'), function (el) {     el.addEventListener('paste', function(e) {         e.preventDefault();         var text = e.clipboardData.getData("text/plain");         document.execCommand("insertHTML", false, text);     }, false); }); 

Saludos.

like image 191
Isaac Limón Avatar answered Sep 21 '22 14:09

Isaac Limón