I am trying to get the carret position inside an iframe whose <body>
has contentEditable = true
. I am running a content script from a chrome addon and binding the action like so:
getCaretPosition = function(element) {
var range = parent.$("iframe.html").getCursorPosition();
alert(range);
return caretOffset;
}
I found the following code for getCursorPosition
in a similar stack overflow answer:
(function ($, undefined) {
$.fn.getCursorPosition = function() {
var el = $(this).get(0);
var pos = 0;
if('selectionStart' in el) {
pos = el.selectionStart;
} else if('selection' in document) {
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart('character', -el.value.length);
pos = Sel.text.length - SelLength;
}
return pos;
}
})(jQuery);
But it is always returning a position of 0.