title: How to get range of selected text of textarea in JavaScript
body: <p>I am trying to retrieve/find the start point and end point of selection in textarea.</p>
  <p>Here is my code which work fine in <a href="https://en.wikipedia.org/wiki/Mozilla_Firefox" rel="nofollow noreferrer">Mozilla</a> and chrome, but it is not working in <a href="https://en.wikipedia.org/wiki/Internet_Explorer_9" rel="nofollow noreferrer">Internet Explorer 9</a>:</p>
  <pre class="lang-html prettyprint-override"><code>&lt;script type=&quot;txt/javascript&quot;&gt;
      function update(o) {
  
          var t = o.value, s = getSelectionStart(o), e = getSelectionEnd(o);
          alert(&quot;start: &quot; + s + &quot; End: &quot; + e);
      }
  
      function getSelectionStart(o) {
          if (o.createTextRange) {
              var r = document.selection.createRange().duplicate()
              rse = r.text.length;
              r.moveEnd('character', o.value.length)
              if (r.text == '')
                  return o.value.length
              return o.value.lastIndexOf(r.text)
          }
          else
              return o.selectionStart
      }
  
      function getSelectionEnd(o) {
          if (o.createTextRange) {
              var r = document.selection.createRang;
              e().duplicate()
              r.moveStart('character', -o.value.length)
              return r.text.length
          }
          else
              return o.selectionEnd
      }
  &lt;/script&gt;
  
  &lt;textarea id =&quot;text&quot; rows=10 cols=&quot;50&quot; onselect=&quot;update(this);&quot;&gt;&lt;/textarea&gt;
  </code></pre>
  <p>When I test this code in Mozilla and Chrome, it gives me correct answer, but when I run this code in Internet Explorer 9, it shows -1 for start and any value for end.</p>
  <p>I want to just find out the start and end point/index of the selected text of the textarea. Actually, the above code works fine for a textbox in all browsers, but not with textarea.</p>
  <p>How can I fix it?</p>