I found the following code here on SO to get the position of the cursor of a contenteditable div, however it always returns 0.
The function that should retrieve the position:
new function($) {
$.fn.getCursorPosition = function() {
var pos = 0;
var input = $(this).get(0);
// IE Support
if (document.selection) {
input.focus();
var sel = document.selection.createRange();
var selLen = document.selection.createRange().text.length;
sel.moveStart('character', -input.value.length);
pos = sel.text.length - selLen;
}
// Firefox support
else if (input.selectionStart || input.selectionStart == '0')
pos = input.selectionStart;
return pos;
}
} (jQuery);
The code I use to test it:
$('div.MESSAGE_OF_DAY').keyup(function() {
alert($(this).getCursorPosition()); // always returns 0???
});
I'm using Chrome (8.0.552.215) if it matters.