Textarea toggle selection tags with Javascript
问题 I'm working on a BBcode editor, and was wondering how to toggle tags on selection using Javascript (similar to how the text editing menu works on StackOverflow). Example: Textarea Lorem ipsum dolor sit amet. If you select the text 'ipsum dolor' and click on the bold button, it'll become Lorem [b]ipsum dolor[/b] sit amet. If you click the bold button once again, it'll remove the tags. Thanks! 回答1: You can get the selected text caret positions, set the caret and then insert your text: TL;DS