Skip to content

Instantly share code, notes, and snippets.

@tigran-sn
Created April 26, 2019 12:55
Show Gist options
  • Save tigran-sn/91561cbc6c716ec6de9ddaa2c5367102 to your computer and use it in GitHub Desktop.
Save tigran-sn/91561cbc6c716ec6de9ddaa2c5367102 to your computer and use it in GitHub Desktop.
Paste only plain text (JavaScript)
function isIE(){
return ua.indexOf('MSIE ') > 0 || ua.indexOf('Trident/') > 0;
}
document.querySelector("[contenteditable]").addEventListener("paste", function (e) {
e.preventDefault();
let text = "";
if (isIE()) {
text = window.clipboardData.getData('text');
e.target.innerText += text;
} else {
text = e.clipboardData.getData('text/plain');
document.execCommand("insertHTML", false, text);
}
if (text !== null && text.trim().length > 0) {
$(this).siblings(/* Fake Placeholder */).hide();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment