Get contentEditable caret position

The following code assumes: There is always a single text node within the editable <div> and no other nodes The editable div does not have the CSS white-space property set to pre If you need a more general approach that will work content with nested elements, try this answer: https://stackoverflow.com/a/4812022/96100 Code: function getCaretPosition(editableDiv) { var … Read more

How to set the caret (cursor) position in a contenteditable element (div)?

In most browsers, you need the Range and Selection objects. You specify each of the selection boundaries as a node and an offset within that node. For example, to set the caret to the fifth character of the second line of text, you’d do the following: function setCaret() { var el = document.getElementById(“editable”) var range … Read more