I am coding a WYSIWYG editor (can’t use something like TinyMCE – have to code myself) and I want users to be able to set the text as bold, underlined, links, etc by adding in tags to the HTML. The problem I’ve got is that when users select the text in the editable div and then click the ‘Bold’ button it is unable to find the selection because it has been deselected because the click event has happened. How can I stop this? Is there a better way to do this?
Share
This function can help you:
Also see my jsfiddle.
=== UPDATE ===
If your button isn’t an
<input type="button/submit" ... />or a<button ...>, you have to remember the selected text after each click:On button click the selected text is in sSelectedText:
Also see my new jsfiddle.