As I understand it, an element with contentEditable="true"
is some kind of WYSIWYG HTML editor. It generates relevant HTML tags corresponding to the command issued.
For example, if one selects text and then presses Ctrl+B, the selected text is placed between <b></b>
tags.
I need to have no style tags in the resulting text. How does one suppress, hijack or control the behavior of those commands?
Other things I could do:
contentEditable
at all but a textarea
instead. But among other things, contentEditable
makes it really easy to highlight the paragraph that is being edited. That's much more difficult to do with a textarea
.contenteditable="false" Indicates that the element is not editable. contenteditable="inherit" Indicates that the element is editable if its immediate parent element is editable.
Its totally secure. The contenteditable attribute is an enumerated attribute whose keywords are the empty string, true, and false.
The contenteditable attribute specifies whether the content of an element is editable or not. Note: When the contenteditable attribute is not set on an element, the element will inherit it from its parent.
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.
Rather than trying to suppress the unwanted tags via JavaScript, I just style them away and save/restore the un-styled text in the contenteditable region.
Update : Added pasted image suppression.
[contenteditable] { background: #eee; width: 15rem; height: 4rem; padding: 1em; } [contenteditable] b { font-weight: normal; } [contenteditable] i { font-style: normal; } [contenteditable] img { display: none; }
<div contenteditable></div>
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