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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy variable value to clipboard

Status
Not open for further replies.
Sep 27, 2001
179
AU
Hi

I was looking at an earlier thread that will copy a textbox contents to clipboard, but what I would like to do is copy a variable value to clipboard. How can this be done.

The script from the earlier post is as follows:

[script]
<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>
[/script]
 
If you want hard enough to do that and to do with the same spirit as you had advised. This is how you do it.
[tt]
<html>
<head>
<script language="javascript">
function toClipBoard(id){
r = document.getElementById(id).createTextRange();
r.select();
r.execCommand("COPY");
}
function copyit(x) {
var oelem=document.createElement("input");
with (oelem) {
id="fld";
name="txt";
type="text"
//style.size="72"; //ie only from the beginning already
//readOnly=true //not essential
value=x;
}
document.forms["display"].appendChild(oelem);
toClipBoard(oelem.id);
document.forms["display"].removeChild(oelem);
oelem=null;
}
</script>
</head>
<body>
<form name="display">
<input type="text" name="abc" value="xyz" />
</form>
<div>Click the button below will copy the input text box value to Clipboard. Thd functionality is supported by ie only.</div>
<button onclick="copyit(document.display.abc.value)">Click to copy</button>
</body>
</html>
[/tt]
As a side note: you can use a less intrusive method as shown here to do the original functionality of putting a fixed string to clipboard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top