Search code examples
javascripttextrangetextrange

How to track the selected text and mark it?


I need to know: with some of what position people select the text and add text around it full of tag with his class.

Wherein: report should be based on HTML tags without such as span and link (a)

Example: enter image description here


Solution

  • I marked this as a duplicate but the comment afterwards meant a response may be needed.

    If you look at the only answer here: Get the highlighted text position in .html() and .text()

    you will find that there is mention of

    Reference

    .wrap()

    .before()

    .after()

    You should be able to use those along with the second part of the answer to add in any tags you want to the selected text.