Wrap bb code around selected text in a textarea
<p>Hello I've found <a href="http://stackoverflow.com/questions/9398574/wrap-bb-code-around-selected-text-in-a-content-editable-div">this</a> thread about my case, but the suggested code is for editable div.
<a href="http://jsfiddle.net/skram/73YXM/" rel="nofollow">DEMO</a></p>

<pre><code>function wrap(tag) {
var sel, range;
var selectedText;
if (window.getSelection) {
    sel = window.getSelection();

    if (sel.rangeCount) {
        range = sel.getRangeAt(0);
        selectedText = range.toString();
        range.deleteContents();
        range.insertNode(document.createTextNode('[' + tag + ']' + selectedText + '[/' + tag + ']'));
    }
}
else if (document.selection &amp;&amp; document.selection.createRange) {
    range = document.selection.createRange();
    selectedText = document.selection.createRange().text + "";
    range.text = '[' + tag + ']' + selectedText + '[/' + tag + ']';
}}
</code></pre>

<p>So how can I use this for &lt; textarea > ?
Thanks in advance.</p>

<p>EDIT:
I tryed this code and it works just fine BUT not in Internet Explorer (tested in v8).</p>

<pre><code>function listText(elementID, openTag, closeTag) {
var textArea = $('#' + elementID);
var len = textArea.val().length;
var start = textArea[0].selectionStart;
var end = textArea[0].selectionEnd;
var selectedText = textArea.val().substring(start, end);
var replacement = openTag + selectedText + closeTag;
textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));}

$(document).ready(function () {
$("#BoldIt").click( function() {
listText("commentText", "[b]", "[/b]");
}); });

&lt;div id="BoldIt"&gt;B&lt;/div&gt;
</code></pre>

<p>In IE if I select word "bug" and clikc on the Bold button the input gets "bug[b]bug[/b]bug</p>
