You can select a part of a web page with the mouse.
I know that I can get the currently selected text but how can I get the DOM element which contains the start or end of the current selection?
You can select a part of a web page with the mouse.
I know that I can get the currently selected text but how can I get the DOM element which contains the start or end of the current selection?
The following will return the container element of the start or end boundary of the current selection, using the boolean
isStart
to specify whether you want the start or end boundary. It will work in most mainstream browsers. Add feature tests for more robustness.In IE, use document.selection.createRange().parentElement() and in real browsers use window.getSelection().getRangeAt(0).startContainer.parentNode. Something like this: