Well, firstly, you had the wrong html, something like this
<a href="" class="format">test</div>
Secondly, when you tried to click the test, it did not select the selected text, because this is what happens if you click somewhere when you have the selected text. Therefore, with that in mind, I changed it to $("body").keypress() so that it wraps the selected text in the gap when the key is pressed. Also, some of jQuery and voila code have been fixed, it works!
Check here .
If you fix your anchor tag and your jQuery $(".contenttext").contents() where .contents() does not exist and the function <
$(".contenttext").html($(".contenttext").html().replace(highlight, spn));
it works as expected here .
source share