Skip to content

Instantly share code, notes, and snippets.

@rozzy
Created December 15, 2012 22:43
Show Gist options
  • Save rozzy/4300019 to your computer and use it in GitHub Desktop.
Save rozzy/4300019 to your computer and use it in GitHub Desktop.
function getCaret(node) {
if (node.selectionStart) {
return node.selectionStart;
} else if (!document.selection) {
return 0;
}
var c = "\001",
sel = document.selection.createRange(),
dul = sel.duplicate(),
len = 0;
dul.moveToElementText(node);
sel.text = c;
len = dul.text.indexOf(c);
sel.moveStart('character',-1);
sel.text = "";
return len;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment