JavaScript: function "getSelection()" gets the selected text, is there any possibility to get the selected text with its HTML tags?

Good day!

Function "getSelection()" gets the selected text, is there any possibility to get the selected text with its HTML tags?

For example, on the page there is a paragraph:

<p>Hello World!</p>

Function "getSelection ()" will get only the text "Hello World!", and how to get it "<p>Hello World!</p>"?

I would be grateful for help.
June 8th 19 at 16:31
2 answers
June 8th 19 at 16:33
Actually no, window.getSelection() returns no text and object Selection. And if you are trying to work with text, it automatically works its toString() method and you will get text only "Hello World".

If you look at the documentation, then there are
Selection.anchorNode // Returns the Node in which the selection begins.

and also
Selection.focusNode // Returns the Node in which the selection ends.

Everything is so complicated because the selection may start in the middle of one tag, and end in the middle of another, something like this:
QIuj5uB.png
June 8th 19 at 16:35
something like that
window.getSelection().anchorNode.parentNode
Unfortunately, it returns "[object HTMLSpanElement]", and I need the text together with tags... - sheldon_McCullough16 commented on June 8th 19 at 16:38
object HTMLSpanElement
so SPAN is not your tags?
515234a52f.jpg - Willie.Berge commented on June 8th 19 at 16:41

Find more questions by tags JavaScript