get selected input text

by edwardsharp

HTML

<textarea id="test" rows="10" cols="50">Some text, yo!</textarea>
<br />
<input type="button" onmousedown="alert(isTextSelected(document.getElementById('test')));" value="What's Selected?">

JavaScript

function isTextSelected(input) {
  if (typeof input.selectionStart == "number") {
    if (input.selectionEnd - input.selectionStart > 0) {
      var text = input.value;
      return text.substring(input.selectionStart, input.selectionEnd);
    } else {
      return 'N0THING!';
    }
  }
  //else if (typeof document.selection != "undefined") {
  //    input.focus();
  //    return document.selection.createRange().text == input.value;
  //}

}