Equivalent of selectionStart, selectionEnd, and focus() for contenteditable spans and divs

3.1k Views Asked by At

I am trying to set a contenteditable span so that it will on the onkeyup event refill the span with formatted text.

However, when I try this the cursor disappears and I can't type anymore. This is because the formatting program, instead of modifying the text already in the span, erases it all and then writes the formatted text in its place.

It is important (I think it is) to note that the element does not appear to be losing focus. Also, because I would like for this project to remain fairly "secret" until it's release, I would rather not give away the source code right now.

1

There are 1 best solutions below

1
On

This is probably due to you using the innerHTML to set the formatted text.

Instead use childNodes collection to access the content and replace textNodes with formatted html element. This avoids setting innerHTML and avoids loosing focus.