Well, although you have found a solution to the problem outlined in your second paragraph, I donβt think the answer to your main question has been given. :)
The Selection object has a property called anchorOffset , giving what you requested (the position of the selected text inside the element). The link above talks about which browsers support it, I'm afraid IE <9 cannot.
function show_selected() { var sel = selection(); console.log(sel.anchorOffset + ':' + sel); }
Now, if you show_selected to, say, mouseup , you will see the offset and selected text printed on the js console.
The frontsia selection may be as follows, supposedly cross-browser:
function selection() { var sel; if(window.getSelection){ sel = window.getSelection() } else if(document.getSelection){ sel = document.getSelection() } else if(document.selection){ sel = document.selection.createRange() } return sel }
source share