As the title says, I'm trying to find the caret position of the user within a content editable div, after deselecting through clicking in the selected area.
While getting the caret position from generating a range based on properties like anchor/focus nodes and anchor/focus offsets from the Document.getSelection() call works fine in most cases, I noticed that when I highlight a block of text and deselect by clicking within the selection range, function calls associated to (for example) the 'mouseup' event still believe that selection range is selected during this time.
This is visibly correct if you were to put a break point inside the function called upon 'mouseup' (in this case showCaretPos() in the below code), the function being called on 'mouseup' while the text is still in it's selected state.
However upon running a function to check the position of the caret again after the 'mouseup' associated function is fired, we get the correct offset for where the caret should be.
Afterwards the caret is placed where the user clicked within their selection and what I'd like to know is if there is a way to find out where the browser plans to place the caret after de-selection happens in terms of nodes and offsets.
Essentially the second example linked, but put together for pasting convenience:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<title>Sandbox</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>
Non-editable text. Editable is below:
<div id="test" contenteditable="true">Hello, some <b>bold</b> and <i>italic and <b>bold</b></i> text</div>
<div id="caretPos"></div>
</body>
<script>
function getCaretCharacterOffsetWithin(element) {
var caretOffset = 0;
var doc = element.ownerDocument || element.document;
var win = doc.defaultView || doc.parentWindow;
var sel;
if (typeof win.getSelection != "undefined") {
sel = win.getSelection();
if (sel.rangeCount > 0) {
var range = win.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(element);
preCaretRange.setEnd(range.endContainer, range.endOffset);
caretOffset = preCaretRange.toString().length;
}
} else if ( (sel = doc.selection) && sel.type != "Control") {
var textRange = sel.createRange();
var preCaretTextRange = doc.body.createTextRange();
preCaretTextRange.moveToElementText(element);
preCaretTextRange.setEndPoint("EndToEnd", textRange);
caretOffset = preCaretTextRange.text.length;
}
return caretOffset;
}
function showCaretPos() {
var el = document.getElementById("test");
var caretPosEl = document.getElementById("caretPos");
caretPosEl.innerHTML = "Caret position: " + getCaretCharacterOffsetWithin(el);
}
document.body.onkeyup = showCaretPos;
document.body.onmouseup = showCaretPos;
</script>
</html>
Example 1 from question Get a range's start and end offset's relative to its parent container
JSFiddle:http://jsfiddle.net/TjXEG/900/
Example 2 from question Javascript: How to detect if a word is highlighted
JSFiddle http://jsfiddle.net/timdown/SW54T/
Example 3 from question Get caret position in contentEditable div