S
S
Shimpanze2018-02-03 12:05:45
JavaScript
Shimpanze, 2018-02-03 12:05:45

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

2 answer(s)
S
Stalker_RED, 2018-02-03
@Stalker_RED

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:
QIuj5uB.png

A
Andrey Tsvetkov, 2018-02-03
@yellow79

something like this
window.getSelection().anchorNode.parentNode

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question