Unfortunately, there is no instant solution for that problem. To solve this you will need to use var sel = window.getSelection()
to get an instance of a Selection object which represents the selected elements. From there, you can use sel.anchorNode()
to get the element where the selection starts and sel.focusNode()
to get the element where it ends. Keep in mind, these will be instances of TextNode and will actually be children of the span that you want.
You will have to come up with a way to determine the shortest path between the two nodes (anchor and focus) which will give you all the information you need.
EDIT: Oh, btw, this is the mozilla/webkit implementation. IE is a completely different beast. You will need to use var sel = document.selection
. Check out How can I get the DOM element which contains the current selection? for more information on how to do this in a cross browser way