Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q356 Can I grab the selected text from a page?

irt.org | Knowledge Base | JavaScript | Text | Q356 [ previous next ]

Q356 Can I grab the selected text from a page?

Only in Netscape Navigator 4:

<P>Netscape and Netscape Navigator are registered trademarks of Netscape Communications Corporation in the United States and other countries. Netscape's
logos and Netscape product and service names are also trademarks of Netscape Communications Corporation, which may be registered in other countries.
Other product and brand names are trademarks of their respective owners. 

<SCRIPT LANGUAGE="JavaScript1.2"><!--
if (document.layers) {
    document.write('<FORM NAME="myform">');
    document.write('<TEXTAREA NAME="mytext" ROWS="10" COLS="30" WRAP="VIRTUAL"><\/TEXTAREA>');
    document.write('<P><INPUT TYPE="BUTTON" VALUE="Copy" onClick="document.myform.mytext.value = document.getSelection()">');
    document.write('<\/FORM>');
}
//--></SCRIPT>

The following was submitted by Galvin Doyle

In Internet Explorer 4+ you can do the following (which has much more power beyond this demonstration):

<script language="JavaScript"><!--
function getext() {
  var t = document.selection.createRange();
  var vt = t.text;
  alert(vt);
  document.form.text.value = vt;
} 
//--></script>

<!--Then in the body of your page-->

<form name="form" method="post" action="">
  <textarea name="text"></textarea>
  <INPUT TYPE="BUTTON" VALUE="Select Text" onClick="getext()">
</form> 

<P>Some text to play with</p>

Feedback on 'Q356 Can I grab the selected text from a page?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.