I have div with contenteditable:
<div class="example" contenteditable="true">This is an example</div>
Using innerHTML
, I get "This is an example".
Now at 7th position (right after "is"), I want to add a html (<span class="yup">NOT</span>
) to give the following (either using click
, focus
what-not)
<div class="example" contenteditable="true">This is <span class="yup">NOT</span> an example</div>
Knowing the caret position and innerHTML, how would I add a html into it?
I thought about using substr
then .html(content)
, but that's replacing the whole content and I want to avoid it. I want to simply add a content directly at a specific position.
For example: Replacing the whole content and what I DON'T want to do.
c = c.substr(0, e) + '<span class="yup">NOT</span>' + c.substr(e)';
$("div.example").html(c);
Instead of replacing it, I want to just "inject" it.
Any suggestions will be much appreciated.