Replace selected text with jquery / javascript

I am trying to create a special specialized WYSIWYG text editor in a browser and has a very limited set of functions, but most of this is transferring certain text to span tags.

I can find many resources explaining the standard stuff (execCommand and nothing else), but looked and looked and can not find anything to do what I need.

In principle, it is as simple as it seems: the user selects some text, presses a button or something else, and the text is replaced with some other text (the original case is the same text enclosed in some HTML tags).

I can find ways to do this in the text box, but I'm just in the usual HTML environment, with the content in question, in a div with contentEditable marked as true.

I also found ways to replace all occurrences of the text or the first occurrence, but not specific. Most of the solutions I find fail when trying to replace anything other than the first appearance.

I hope jQuery can do this in some way.

+5
source share
2 answers

Have you tried using the jQuery wrapSelection plugin?

+2
source

This is similar to this question . This can help.

+1
source

All Articles