Consider the following contenteditable div.
<div contenteditable="true"> <div>bold text</div><div>bold text</div> </div>
If I place the cursor between two divs and start typing, it appears in bold instead of inserting new node text between the two divs. The same thing happens if you get home and try to enter something before the first div. It becomes part of the first div.
If I check the startContainer of the range returned from the selection, I get the content for one of the div instead of the empty node text, as expected.
See http://jsfiddle.net/9ZZpX/3/
The question is, why is this happening? How can I choose a place between divs so that when I enter something it is not highlighted in bold? (Obviously, I can add a space, and this works around the problem, but it's pretty ugly.)
You can see how this works properly on Facebook if you type @mention in the status update field and press HOME. If you type, it will not be highlighted.
The only thing I could think of was to intercept the keystroke and insert the node text programmatically, but it seems ugly.
I searched like crazy and cannot find any links confirming how this should really work. Obviously, something that I do not understand, and the documentation in this area is really not enough.
(What I want to do also detects when the cursor is about to enter one of these divs and jump over it. If the two divs are next to each other, the cursor jumps to one of the divs and it twists the work.)
Further information on what I'm trying to do: http://a-software-guy.com/2012/12/the-horrors-of-cursor-positioning-in-contenteditable-divs/
javascript dom range cursor contenteditable
Yermo lamers
source share