My example:
<div contenteditable="true">
Hello <span class="tagname">Hermione</span> hello @
</div>
When I get the text of the div:
let text = $('[contenteditable=true]').text(); // 'Hello Hermione hello @'
The index of @
character is: 21
.
I want to insert more span tag inside the div via the index. But how can I do that without losing already tag(s)?
My idea:
let $div = $('[contenteditable=true]'), text = $div.text(),
tagname = $('<span>').addClass('tagname').text('voldemort');
$div.html(text.substring(0, index))
.append(tagname)
.append(text.substring(index + 1, text.length));
If I use that way, all already tag(s) would be overridden. I will lose them.
Also, I cannot use:
$div.html($div.html().replace('@', tagname[0].innerHTML));
because the content may contain some @
character(s) that I don't want to replace (must be via index).
Any idea about this problem?