0

my <span class="highlight">highlighted</span> word

In above selected text include html tag. When I use window.getSelection() returns only the text without tag my highlighted word but I need entire selected text with <span> tag as above. Could anyone please help me.

Priyanka
  • 63
  • 2
  • 6

3 Answers3

0

In order to get the inner HTML of a given tag you shoul try to use innerHTML prop. EG:

For a tag:

<p id="high">My <span class="highlight">Highlighted</span></p>

You can store the node in a variable like:

const text = document.getElementById("high");

And if you access the prop innerHTML it will return you the value inside the p tag

console.log(text.innerHTML)

Result: "My <span class="highlight">Highlighted"

Ed Girão
  • 11
  • 2
0

Can try something like this

   window.getSelection().anchorNode.parentNode
0

I got solution by using

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

Priya
  • 3
  • 6