I’m trying to append an <a> tag in to selected text when user right click on it.i searched over the stack , no matches found.
I’m trying to append an <a> tag in to selected text when user right
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Dealing with selected text reliably is a bit tricky cross-browser. Tim Down’s library Rangy can be useful there, he’s smoothed over a lot of the browser idiosyncrasies. (Even if you don’t want to use the library, you can study it for the techniques.)
One of the core Rangy demos is surrounding selected text with an element, using the
RangyRange#surroundContentsmethod. The code for that in the demo looks like this:You’d be doing much the same, but with an
arather than aspan.