I have a contentEditable Div and I want remove any formatting especially for copy and paste text.
Just set contentEditable="false" . See this answer.
Answer: Use the HTML5 contenteditable Attribute You can set the HTML5 contenteditable attribute with the value true (i.e. contentEditable="true" ) to make an element editable in HTML, such as <div> or <p> element.
To prevent contenteditable element from adding div on pressing enter with Chrome and JavaScript, we can listen for the keydown event on the contenteditable element and prevent the default behavior when Enter is pressed. to add a contenteditable div. document. addEventListener("keydown", (event) => { if (event.
Changing your tabIndex to >= 0 will let you focus on the elements.
document.querySelector('div[contenteditable="true"]').addEventListener("paste", function(e) { e.preventDefault(); var text = e.clipboardData.getData("text/plain"); document.execCommand("insertHTML", false, text); });
It is simple: add a listener to the "paste" event and reeformat clipboard contents.
Here another example for all containers in the body:
[].forEach.call(document.querySelectorAll('div[contenteditable="true"]'), function (el) { el.addEventListener('paste', function(e) { e.preventDefault(); var text = e.clipboardData.getData("text/plain"); document.execCommand("insertHTML", false, text); }, false); });
Saludos.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With