I'm trying to inject <span>
elements into the text a user is typing into a contenteditable
<div>
. Input events (DOMCharacterDataModified
) periodically trigger requests to a service that identifies certain entities in the text, and I'd like to update it accordingly. For example,
<div id="editor" contenteditable="true">
The New York Times is an American newspaper based
in New York City with worldwide influence and
readership.
</div>
becomes
<div id="editor" contenteditable="true">
The <span class="name">New York Times</span> is an
American newspaper based in <span class="loc">New
York City</span> with worldwide influence and
readership.
</div>
Is there any way I can inject <span>
elements without replacing the <div>
's content, thus preserving the user's text cursor position?