Retrieving selected text from textarea when span is clicked in IE
<p>I want to get the selected text within the text area when 'span' is clicked. When I click button the selection works but not when I click on span.<br>
Maybe because the selection is lost when span is clicked, but it's not happening when button is clicked? 
How to fix it?</p>

<pre><code>function Copy() {
  var theSelection = document.selection.createRange();
  alert(theSelection.text);           
}

 &lt;div&gt;
    &lt;span class="Icon" onclick="Copy();"&gt;&lt;/span&gt; &lt;input type="button" value="Copy" onclick="Copy();" style="float:left;" /&gt;  
 &lt;/div&gt;
 &lt;div style="clear:both;"&gt;
    &lt;textarea rows="2" cols="20" style="height:370px;width:800px;"&gt;&lt;/textarea&gt; 
 &lt;/div&gt;
</code></pre>

<hr>

<p><strong>IE only!</strong></p>

<p><a href="http://www.webdevout.net/test?0V&amp;raw" rel="nofollow">Online Example</a></p>

<p>Update:</p>

<p>This is how I do it in firefox:</p>

<pre><code>if (window.getSelection){ // Firefox, Opera, Safari

   var textbox = document.getElementById("box");
   textbox.focus();
   theSelection = document.activeElement.value.substring(document.activeElement.selectionStart, document.activeElement.selectionEnd);

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