I have a contenteditable <div>
area, and within this area I may have some <span contenteditable="false"></span>
containing some text. The idea is, these span elements will represent styled text that can not be edited, but may be deleted from the <div contenteditable="true"></div>
area by pressing the backspace key.
The cursor placement is the big issue here. if you delete one of these <span>
elements, the cursor jumps to the end of the <div>
. More interesting, if you type some text while the cursor is "at the end," the text placement is just fine... Then, if you delete the newly typed text, the cursor jumps back!
I have prepared a fiddle which will demonstrate this. I need this to work only in Chrome, and other browsers are either of non-concern for now or have workarounds in place. (Also note the prepared Fiddle is crafted to demonstrate this in Chrome only).
Fiddle
Fiddle Instruction: Chrome Version 39.0.2171.95 m (64-bit) reproduced in 32-bit as well
<div>
areaResearching this extensively, I have come across various SO question that are similar, but borrowing the associated solutions has not proved to be the silver bullet I am after. I have also found issues for the Chrome project which seem to target (perhaps not in the exact manner) the issue described above, and can be viewed below.
The closest SO solution I have found can be here. The idea in this solution is to place ‌
characters after the <span>
elements, but if I want to now delete my <span>
, I instead delete the ‌
... forcing my cursor to jump to the end, offering a weird UI experience by not deleting my <span>
on my "initial delete key stroke."
Has anyone experienced this issue and found a work around? I welcome any possible solution, even as JS hacky as they come. I've come to learn that leveraging contenteditable
comes with a laundry list of struggle, but this seems to be the last remaining difficulty I currently have.
To move the cursor to the end of an input field: Use the setSelectionRange() method to set the current text selection position to the end of the input field. Call the focus() method on the input element. The focus method will move the cursor to the end of the input element's value.
Just set contentEditable="false" . See this answer.
The contenteditable global attribute is an enumerated attribute indicating if the element should be editable by the user. If so, the browser modifies its widget to allow editing.
I don't know why this happens, but I had the feeling it has something to do with the sizing of the <div>
, so I tried playing with the display
property. After setting it to inline-block
and playing a little with the text I found that the issue is gone after I make some edits to it, specifically adding a new line.
I saw that, for some reason, the <br/>
tag is kept in div.main
after I delete my new line, but the appearance of the <div>
and the way it responds to arrow keys is the same as if there is no new line in it.
So I restarted the fiddle with the CSS change and a <br/>
tag in div.main
and viola!
So to conclude:
display: inline-block
to div.main
<br/>
at the end of div.main
JSFiddle Link
The problem is that your contenteditable
element is a div
, which by default is display: block
. This is what causes your caret position problem. This can be fixed by making the outermost div
non-editable and adding a new editable div
that will be treated as inline-block
.
The new HTML would have a new div
just inside the outer one (and the corresponding closing tag at the end):
<div id="main" class="main"><div id="editable" contenteditable="true">...
And add this to your CSS:
div#editable {
display: inline-block;
}
For the sake of seeing the caret better when it is between span
elements, I've also added margin: 2px
to the rule for div.main span
in the CSS but this is not necessary to prevent the caret jumping issue reported in the question.
Here is a fiddle.
As you've started discovering, contenteditable
is handled inconsistently across browsers. A few years back, I started working on a project (in-browser XML editor) where I thought contenteditable
would make everything easier. Then as I developed the application, I soon found myself taking over the functions that I thought contenteditable
would give me for free. Today, the only thing contenteditable
give me is that it turns on keyboard events on elements I want to edit. Everything else, including caret movement and caret display, is managed by custom code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With