2

I'm not familiar with such attributes,

can someone provide a simple demo?

I need it to be done without any libraries.

omg
  • 123,990
  • 135
  • 275
  • 341
  • 3
    Stack Overflow Archive - http://stackoverflow.com/questions/717224/how-to-get-selected-text-in-textarea - http://stackoverflow.com/questions/275761/how-to-get-selected-text-from-textbox-control-with-js --- **Hint**: Before asking a question, look to Google: `site:stackoverflow.com javascript get selected text within textarea` returns only relevant material from stackoverflow.com. Or you can type the same thing into your address bar following a ? to go to the first result. Give it a shot, type this into your address bar: `?site:stackoverflow.com javascript get selected text within textarea` – Sampson Jun 29 '09 at 12:35

1 Answers1

6
<script type="text/javascript">

        function ShowSelectionInsideTextarea()
{
 var textComponent = document.getElementById('mytextarea');

  var selectedText;
  // IE version
  if (document.selection != undefined)
  {
    textComponent.focus();
    var sel = document.selection.createRange();
    selectedText = sel.text;
  }
  // Mozilla version
  else if (textComponent.selectionStart != undefined)
  {
    var startPos = textComponent.selectionStart;
    var endPos = textComponent.selectionEnd;
    selectedText = textComponent.value.substring(startPos, endPos)
  }
    alert("You selected: " + selectedText);

}
</script>
user2851996
  • 77
  • 1
  • 4