So, I'm trying to create an RTE environment. I have editable content div, and I would like to allow the user to select the text, and then click on the button that wraps the BBCode around it.
I tried to create the following function. However, the selected text is simply replaced. It does not seem to store the correct ein selectedText value
function wrap(tag) { var sel, range; if (window.getSelection) { sel = window.getSelection(); if (sel.rangeCount) { range = sel.getRangeAt(0); var selectedText = range; range.deleteContents(); range.insertNode(document.createTextNode('['+tag+']'+selectedText+'[/'+tag+']')); } } else if (document.selection && document.selection.createRange) { range = document.selection.createRange(); selectedText = document.selection.createRange().text; console.log(text); range.text = '['+tag+']'+text+'[/'+tag+']'; } } </script>
JQuery is valid, but I would prefer plain Javascript.
source share