I am trying to get the starting and ending points of user selection. This is the content of a DIV that I have
abc def ghi jkl mno pqr stuv wxyz
When I select pqr
The starting point becomes 1 and the ending point becomes 4 which is wrong
When I select the whole thing, the starting point becomes 0 and the ending point becomes 33 which is right
I have the following code:
sel = window.getSelection();
le = sel.toString().length;
if (sel.getRangeAt && sel.rangeCount)
{
range = window.getSelection().getRangeAt(0);
sp = range.startOffset;
ep = sp + le;
}