This question already has an answer here:
- Return HTML from a user-selected text 3 answers
With a contenteditable element how can I replace the selected content with my own html?
This question already has an answer here:
With a contenteditable element how can I replace the selected content with my own html?
To get the selected HTML, you can use the function I wrote for this question. To replace the selection with your own HTML, you can use this function. Here's a version of the replacer function that inserts an HTML string instead of a DOM node:
See here for working jsFiddle: http://jsfiddle.net/dKaJ3/2/
Code taken from Tim Down: Return HTML from a user-selected text