Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  172] [ 2]  / answers: 1 / hits: 43346  / 15 Years ago, wed, august 26, 2009, 12:00:00

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?


More From » html

 Answers
70

In IE, use document.selection.createRange().parentElement() and in real browsers use window.getSelection().getRangeAt(0).startContainer.parentNode. Something like this:



function getSelectedNode()
{
if (document.selection)
return document.selection.createRange().parentElement();
else
{
var selection = window.getSelection();
if (selection.rangeCount > 0)
return selection.getRangeAt(0).startContainer.parentNode;
}
}

[#98831] Thursday, August 20, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarrettw

Total Points: 300
Total Questions: 98
Total Answers: 103

Location: Saudi Arabia
Member since Mon, Sep 5, 2022
2 Years ago
;