execCommand('Copy') just for a value

During script executionI get javascript variable v=100
I'd like to copy this value to the clipboard.
I see on internet a lot of exmaples of combination like of

Copied = hold.createTextRange();
Copied.execCommand('Copy');

But I need just one (string) value to be copied to the clipboard nothing more.
And still didn't get it done .....
APRESTOUSAsked:
Who is Participating?
 
yaxhConnect With a Mentor Commented:
One thing I wanna ask here is: Is your variable 'String'?

If it is string then you can use it as it is i.e. window.clipboardData.setData('Text',mytext);  otherwise you have to use this window.clipboardData.setData('Text',mytext.value);
0
 
yaxhCommented:
// set the clipboard
 var x = 'Whatever you want on the clipboard';
// Copy to clipboard
 window.clipboardData.setData('Text',x);
// get the clipboard data
window.clipboardData.getData('Text');
// clear the clipboard
 window.clipboardData.clearData();

Read more : http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/clipboarddata.asp
0
 
raj3060Commented:
try this:

holdtext.innerText = copytext.innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");

You need to define what text you want to copy..
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ZvonkoSystems architectCommented:
Copy the varible into an temporary storage on the page and transfere it from there to clipboard. Here an example: http:Q_20233392.html
0
 
raj3060Commented:
and here is a working code:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">

function ClipBoard()
{
holdtext.innerText = copytext.innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
}

</SCRIPT>
</HEAD>
<BODY>

<SPAN ID="copytext" STYLE="height:150;width:162;background-color:silver">
This text will be copied onto the clipboard when you click the button below. Try it!
</SPAN>

<TEXTAREA ID="holdtext" STYLE="display:none;">
</TEXTAREA>
<BUTTON onClick="ClipBoard();">Copy to Clipboard</BUTTON>

</BODY>
</HTML>
0
 
APRESTOUSAuthor Commented:
This didn't work...

function CopyId(h) {
    var x = h
    window.clipboardData.setData('Text',x);
}

0
 
APRESTOUSAuthor Commented:
I wroote that I see on Internet solutions
with  createTextRange();

But I don't get how i can use it for variable

I have a function
function CopyId(h) {
}

that receive value as argument. I'd like just and only this variable to be copied into clipboard
0
 
yaxhCommented:
I tested following code and its working fine on IE7:

<html>
<head>
<title>Test</title>
<script language="javascript" type="text/javascript">
function copyText(mytext) {
if (!document.all) return; // IE only
window.clipboardData.setData('Text',mytext);
}
</script>
</head>
<body>
<form>
<button onclick="copyText('WhaterTextYouWantToCopy');return false;" >Copy</button>
</form>
</body>
</html>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.