Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contenteditable allowing only plain text [duplicate]

I'm trying to make a contenteditable that only accepts plain text.

What I want is to listen to paste event and then remove all html and paste it on the contenteditable only as plain text.

In order to do that I've already tried two different approachs, based answers to similar questions, but those two approachs have problems.

First: This one isn't using "paste" listener, is instead listening for input (not ideal for this case), this was a solution to avoid using Clipboard API.

Problem: This works fine on chrome but not on firefox and IE, when you copy and paste the html it sucessfully removes html and only paste text, but when continuing to write text, the caret is always taken to the beginning of the contenteditable.

function convertToPlaintext() {   var textContent = this.textContent;   this.textContent = textContent; }  var contentEditableNodes = document.querySelectorAll('.plaintext[contenteditable]');  [].forEach.call(contentEditableNodes, function(div) {   div.addEventListener("input", convertToPlaintext, false); }); 

You can try it here/code on which I based: http://jsbin.com/moruseqeha/edit?html,css,js,output

Second: Since the first one failed and isn't listening to "paste" event only, I've then decided to give it a try using Clipboard API. The problem here is that on IE document.execCommand("insertHTML", false, text); doesn't seem to work. This works on chrome, firefox (tested on v41 and v42) and edge.

document.querySelector(".plaintext[contenteditable]").addEventListener("paste", function(e) {   e.preventDefault();   var text = "";   if (e.clipboardData && e.clipboardData.getData) {     text = e.clipboardData.getData("text/plain");   } else if (window.clipboardData && window.clipboardData.getData) {     text = window.clipboardData.getData("Text");   }   document.execCommand("insertHTML", false, text); }); 

You can try it here: http://jsfiddle.net/marinagon/461uye5p/

Can anyone help me find a solution for some of this problems or anyone has a better solution than the ones presented here?

I'm aware that I could use textarea, but I have reasons not to use it.

like image 323
Marina Avatar asked Jan 13 '16 10:01

Marina


People also ask

What is false about Contenteditable attribute?

contenteditable="false" Indicates that the element is not editable. contenteditable="inherit" Indicates that the element is editable if its immediate parent element is editable.

How do I turn off Contenteditable div?

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

How do you use Contenteditable in Javascript?

You can add the contenteditable="true" HTML attribute to the element (a <div> for example) that you want to be editable. If you're anticipating a user to only update a word or two within a paragraph, then you could make a <p> itself editable.

What does Contenteditable mean?

The contenteditable attribute specifies whether the content of an element is editable or not. Note: When the contenteditable attribute is not set on an element, the element will inherit it from its parent.


2 Answers

Dirty hack to clean the pasted data.

function onpaste(e, el){       setTimeout(function(){             el.innerText = el.innerText;         }, 10); } 
like image 143
Kristiyan Gospodinov Avatar answered Oct 11 '22 13:10

Kristiyan Gospodinov


Use a textarea element instead of a contentEditable element: (emphasis mine)

The textarea element represents a multiline plain text edit control for the element's raw value.

Example:

/* Auto resize height */  var textarea = document.querySelector('textarea');  (textarea.oninput = function() {    textarea.style.height = 0;    textarea.style.height = textarea.scrollHeight + 'px';  })();
textarea { width: 100%; }
<textarea>Hello! You can edit my content. But only plain-text!!!</textarea>
like image 39
Oriol Avatar answered Oct 11 '22 12:10

Oriol