How to paste text as plain text into a content editable element with JavaScript?

Estimated read time 1 min read

You can paste text as plain text into a content editable element using the Clipboard API and JavaScript. Here’s an example:

document.addEventListener('paste', function(event) {
  let clipboardData = event.clipboardData || window.clipboardData;
  let text = clipboardData.getData('text/plain');
  document.execCommand('insertHTML', false, text);
});

In this example, the event listener is attached to the paste event, and when the event is triggered, the text from the clipboard is retrieved using event.clipboardData.getData('text/plain'). Then, the document.execCommand('insertHTML', false, text) method is used to insert the text as plain text into the content editable element.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply