I've read quite a few posts on here about how document.execCommand
is being deprecated and that the Clipboard API should be used instead. However I can't figure out what to replace it with. I have a div[contenteditable]
with auto-height and I want to convert pasted text into plain text when something is pasted into it. I know it may seem contradictive to use a rich-text element for plain text, but it's by far the best way to have a textarea autogrow that I've found. The following snippet does what I need it to, but again, since everybody is yelling don't use document.execCommand
, what do you suggest I replace it with below? Thanks!
document.body.addEventListener("paste", function (e) {
let pasteElements = ['SPAN', 'DIV', 'BR'];
if (pasteElements.includes(e.target.nodeName)) {
e.preventDefault();
const text = e.clipboardData.getData('text/plain');
document.execCommand("insertText", false, text.trim());
}
});