0

Given HTML like:

<p>
    <span id="text 1"> Hey everyone </span>
    <span id="text 2"> I have a</span>
    <span id="text 3"> question for you.</span>
</p>

and a selection from "everyone" to "have", I can get a Selection object by:

function GetSelectedNodes() {
    if (window.getSelection) {
        var selection = window.getSelection();
        var start_node = selection.anchorNode;
        var end_node = selection.focusNode;
    }
 }

But what I want is a list of the Span elements in the selection. How do I get it?

Lumen
  • 139
  • 11

1 Answers1

1
window.getSelection().getRangeAt(0).cloneContents(); 

Source - window.getSelection() gives me the selected text, but I want the HTML

user4157124
  • 2,809
  • 13
  • 27
  • 42
Nishant
  • 466
  • 2
  • 15