Javascript: Get Selected Text From Textarea in IE8
<p>I'm trying to read in the value of selected text within a text area.  Here is my code:</p>

<pre><code>function readSelected(id)
{ 
    textarea = document.getElementById(id);
    if (document.selection)
    { //Code for IE
        textarea.focus();
        sel = document.selection.createRange();
        alert(sel.text);
    }
    else 
    {  // Code for Mozilla Firefox
        var len = textarea.value.length;
        var start = textarea.selectionStart;
        var end = textarea.selectionEnd;

        var scrollTop = textarea.scrollTop;
        var scrollLeft = textarea.scrollLeft;

        sel = textarea.value.substring(start, end);

        alert(sel);
    }
}
</code></pre>

<p>HTML:</p>

<pre><code>&lt;textarea id="txt1" rows="10"&gt;&lt;/textarea&gt; 
&lt;a onclick="readSelected('txt1');"&gt;Get Selected&lt;/a&gt;
</code></pre>

<p>When you click the button, a popup should occur telling you what the selected text is.</p>

<p>The code works in Firefox, but I can't get it to work in IE8.</p>
