How to paste as plain text with JavaScript execcommand?

Estimated read time 1 min read

You can paste plain text using the document.execCommand() method in JavaScript, like this:

document.execCommand('insertText', false, 'Paste your text here');

You can also retrieve the text from the clipboard and paste it using the Clipboard API, like this:

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

Note that the document.execCommand() method is not supported in all modern browsers and is considered outdated, it is better to use more modern alternatives like the Clipboard API.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply