My situation is, that I have some kind of an inputbox in HTML (right now it's a contentEditable div) element
in which I want to replace one specific word a user can type in, with one other word:
word1 = "hello";
word2 = "<b>hello</b>";
//element = some random element that supports innerHTML
element.oninput = function(){
element.innerHTML = element.innerHTML.replace(word1, word2);
}
So if the user types "hello", it is automatically replace with a bold "hello", which works only if I use an element with the property innerHTML
, so I can insert the <b>
. Now my problem is, that once the innerHTML is replaced (l. 5), the caret jumps back in front of the first letter in the div:
[this is a hell|] -> [|this is a <b>hello</b>]
What can I do to prevent that from happening without using jQuery?
I can also change the element type/class, it just needs to support innerHTML
...