Get caret (cursor) position in contentEditable area containing HTML content

UPDATE I’ve written a simpler version of this that also works in IE < 9: https://stackoverflow.com/a/4812022/96100 Old Answer This is actually a more useful result than a character offset within the text of the whole document: the startOffset property of a DOM Range (which is what window.getSelection().getRangeAt() returns) is an offset relative to its startContainer … Read more

How to get selected html text with javascript? [duplicate]

Here’s a function that will get you HTML corresponding to the current selection in all major browsers. It also handles multiple ranges within a selection (currently only implemented in Firefox): function getSelectionHtml() { var html = “”; if (typeof window.getSelection != “undefined”) { var sel = window.getSelection(); if (sel.rangeCount) { var container = document.createElement(“div”); for … Read more