I am trying to find the position of the selected text in a div. I prompt the user to select a substring of the text in a div and then submit it. I need the index of the start point and end point and have not been successful using the strategy below. Any help would be much appreciated.
This is the function I have and it is not working:
if (typeof window.getSelection != 'undefined') {
var sel = window.getSelection();
var range = sel.getRangeAt(0);
var preCaretRange = range.cloneRange();
startOffset = preCaretRange.toString().length;
endOffset = startOffset + range.toString().length;
}
The div is called "myarea". Thanks