Place a carriage outside an inserted label in ContentEditable

I insert the span tag in the contentEditable div using the document.execCommand insertHTML method.

After the insertion, I want the carriage to be installed at the end of the input, so that any additional input is outside the last inserted span tag. The default behavior, at least in Chrome, should place an additional input inside the tag itself.

+4
source share
2 answers

The terrible hack I came up with is to insert another span immediately after the word:

 document.execCommand('insertHTML', false, '<span>&nbsp;</span>'); 

Now the new content goes into this span element. We hope there is a better solution.

+2
source

I, too, got stuck on this until I tried it. Put &nbsp; at the end of your insert as in

 document.execCommand('insertHTML', false, '<span>&nbsp;</span>&nbsp;'); 

Just placing a space at the end of your HTML file will not work, since I found that it is always trimmed.

+2
source

Source: https://habr.com/ru/post/1440399/


All Articles