code

get selected input text

a button reports the selected part of a text area, or lets you know when nothing is selected.

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;
  //}
}

this demo preserves the compiled output and may depend on external libraries or services that are no longer available.

original jsfiddle post