Answer the question
In order to leave comments, you need to log in
JavaScript: The "getSelection()" function gets the selected text, is it possible to get the selected text along with its HTML tags?
Good afternoon!
The "getSelection()" function gets the selected text, is it possible to get the selected text along with its HTML tags?
For example, there is a paragraph on the page:
The “getSelection()” function will only get the text “Hello World!”, but how to get exactly “ ”?
I will be grateful for help. <p>Hello World!</p>
<p>Hello World!</p>
Answer the question
In order to leave comments, you need to log in
Actually no, window.getSelection() does not return text, but a Selection object . And if you try to work with it as with text, then its toString() method will automatically work and you will get "only Hello World text".
If you look at the documentation, there is
and also
Everything is so complicated because the selection can start in the middle of one tag and end in the middle of another, something like this:
something like thiswindow.getSelection().anchorNode.parentNode
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question