How to get HTML selection and parent selection?

A few years ago, I added smart quoting to a web forum. Basically, the user selects part of a previous conversation and presses a button to quote it. Script gets the HTML code for the quote and goes up the DOM tree to find out who said it.

I could only do this for IE, although I remember trying a lot. But then there was no stackoverflow.com, and Firefox was not so mature. I think that doing it now in Firefox is just as easy. Here is the key part of the code.

range2Copy = frameDoc.selection.createRange(); 
html2Copy = range2Copy.htmlText; 

el = range2Copy.parentElement();

// go up the HTML tree until post row node (id=postrowNNNN)

while (el.nodeName != 'BODY' &&
        !el.id.match(/postrow/)) {

    el = el.parentNode;
}

The frameDoc element contains the previous stream in which the user selects the text. If that makes little sense, see all the code here . This is a plugin for FCKeditor.

+3
1

, firefox, , , :

var selection = window.getSelection(); 
var node = selection.anchorNode;

while (node.nodeName != 'BODY' && !node.id.match(/postrow/)){
    node = node.parentNode;
}
+5

Source: https://habr.com/ru/post/1708037/


All Articles