Cross Browser Text selection


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <script language=javascript>
  2. function getSelText()
  3. {
  4.     var txt = '';
  5.      if (window.getSelection)
  6.     {
  7.         txt = window.getSelection();
  8.              }
  9.     else if (document.getSelection)
  10.     {
  11.         txt = document.getSelection();
  12.             }
  13.     else if (document.selection)
  14.     {
  15.         txt = document.selection.createRange().text;
  16.             }
  17.     else return;
  18. document.aform.selectedtext.value = txt;
  19. }
  20. </script>
  21. <input type="button" value="Get selection" onmousedown="getSelText()">
  22. <form name=aform >
  23. <textarea name="selectedtext" rows="5" cols="20"></textarea>
  24. </form>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.