title: HTML5 how to get selected text out of a textarea
body: <p>I was able to get the highlighted text out of a textarea by recording onselect and storing the beginning and end each time. Then, when I click a button, I build the substring myself.  Isn't there a simpler way of simply querying the selection?</p>
  
  <p>I was kind of expecting that there would be methods in html5 dom for all these things, something like:</p>
  
  <p>textarea.getSelectedStart()
  textarea.getSelectedEnd();
  textArea.setSelected(start,end);</p>
  
  <p>Also, is there a way of programmatically deselecting text in a textarea? </p>
  
  <p>I am putting in code based on the first solution below.  This sort of works, but has a weird problem:</p>
  
  <pre><code>&lt;script language=javascript&gt;
  function replaceCLOZE(code, questionType) {
    var v = code.value;
    var s = code.selectionStart;
    var e = code.selectionEnd;
    var t = v.substr(s, e-s);
    var rep = "{:" + questionType + ":="+t+"}";
    code.value = v.substr(0,s) + rep + v.substr(e,v.length-e+1);
  }
  
  function shortAnswer(code) {
    replaceCLOZE(code, "SA");
  }
  function shortAnswerCaseSensitive(code) {
    replaceCLOZE(code, "SAC");
  }
  function multipleChoice(code) {
    replaceCLOZE(code, "MC");
  }
  </code></pre>
  
  <p>The text area does in fact have attributes code.selectionStart and code.selectionEnd.  But the code above, which now works, sets the highlighted text on the screen to be the first word in the textarea.  Mind you, the selectionStart is still correct, but what is actually highlighted in Firefox is wrong.</p>
  
  <p>In Chrome it works fine.  Maybe this is just a bug in firefox or is there something else which should be done to properly update the textarea visually?</p>