I’m trying to achieve the following functionality:
<div id="editor" contenteditable="true">
I am some text.
</div>
$('#editor').replaceHtmlAt(start, end, string);
Use case:
-
User types an
@inside#editor -
keyupevent picksup the@position -
$('#editor').replaceHtmlAt(position of @, position of @ + 1, "<div>Hello</div>");
Is this possible?
EDIT
I got it to work by doing this
$(this).slice(pos-1).html('<span id="mention'+pos+'">@</span>');
However I’ve encountered another problem. In Chrome, the caret position inside #editor moves all the way to the back… how do I restore the caret’s position after ‘@’ inside the span tags?
Dylan, although your thinking about replacing ‘@’ is right in layman terms, we (coders) know that we can intercept and mess around with key events.
So, building on what Derek used up here, I’d do:
Demo JSFiddle