Here is a post on working with selected text. The getSelection()
method can be used to get the selected text, then you should be able to replace that text with text wrapped in a tag.
Dave Swersky
2009-12-07 16:13:44