In a contenteditable element
, I have two types of children - span
and regular text.
<div contenteditable="true">I am first. <span class="tag">Mark me and delete me.</span> I am last.</div>
.tag {
background-color: lightblue;
margin-left: 5px;
margin-right: 5px;
}
If I select the text content of a span (in the above example - Mark me and delete me.
) and delete it (pressing backspace or del), the element disappears from the contenteditable parent:
<div contenteditable="true">I am first. I am last.</div>
However, when I type any character again - a span element is recreated. More oddly - some of the styles of the span are preserved, even if they come from a class definition:
<div contenteditable="true">I am first. <span style="background-color: rgb(173, 216, 230);">a</span> I am last.</div>
Is it possible to prevent this reinsertion of deleted elements?
My only idea thus far is to reparse the contenteditable after each keyup
event and replace the faulty new elements. However, this is is clearly inefficient.
I think this problem appears only in Chrome, I couldn't reproduce it in Firefox. Here is a jsfiddle with the example above.