I need to get the user selected region of the text field, and then insert the <a>
tags there.
I use this to get the user selected area:
var textComponent = document.getElementById('article'); var selectedText; if (document.selection != undefined) { textComponent.focus(); var sel = document.selection.createRange(); selectedText = sel.text; }
Now I know that I can perform a string search for the text selected by the user and insert tags around it, but what happens if this text selected by the user appears twice in the text, for example.
Hello, goodbye.
If the user selects a second “you” for the link they want, surely replacing the string will put tags around each instance of “you”.
What is the best way to do this?
source share