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
.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:
[contenteditable] {
background: #eee;
width: 15rem;
height: 4rem;
padding: 1em;
}
[contenteditable] b {
font-weight: normal;
}
[contenteditable] i {
font-style: normal;
}
<div contenteditable></div>