Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy to Clipboard 1

Status
Not open for further replies.

CliveC

Programmer
Nov 21, 2001
1,222
US
I am trying to copy some text in an entry field to the windows clipboard, using some code I found in another thread. It won't work - any ideas? Here is the essential code fragment:

Code:
<html><body>
<form name="display">

<script language="JavaScript" type="text/javascript">
function toClipBoard(id){
  r = document.body.createControlRange();
  r.add(document.getElementById(id));
  r.select();
  r.execCommand("COPY");
}
val="XYZ"
document.writeln("<input name='txt' id='fld' type='text' size='72' readonly value="+val+" />");
toClipBoard('fld')
</script>

</form></body></html>

Clive
 
clivec, change your code to this and it will work:
Code:
<html><body>
<form name="display">

<script language="JavaScript" type="text/javascript">
function toClipBoard(id){
  r = document.getElementById(id).createTextRange();
  r.select();
  r.execCommand("COPY");
}
val="XYZ"
document.writeln("<input name='txt' id='fld' type='text' size='72' readonly value="+val+" />");
toClipBoard('fld')
</script>

</form></body></html>

-kaht

banghead.gif
 
Kaht - I haven't seen createTextRange() - what's it do?

BTW - I guess the site I was hosting our logos on must be gone...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook

 
Hey mike, thanks for the head's up on the sig image.

As for createTextRange, I'm not exactly sure what else it's used for. A long time ago someone asked how to copy something to the clipboard on tek-tips, so I googled and found an example that used createTextRange. Since then it's the only thing I've used it for. However, I think I remember an example that allowed you to position your cursor in a textbox at whatever position you wanted that used createTextRange, but I could be mistaken.

-kaht
 
Looks like I may have to experiment...

BTW - Haven't seen too much of you, guess that you've been busy. Nice to see you.

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook

 
Yeah, I've been pretty busy. My company writes a vacation tracker software for SBC so with the new year starting we had a whole new set of vacation data for 2005. It's kept me pretty occupied. I hop on occasionally just to get a star or 2 and keep my name in the top 10, heh.

-kaht
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top