[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

execCommand('Copy') just for a value

Posted on 2007-08-08
8
Medium Priority
?
3,397 Views
Last Modified: 2013-11-05
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 .....
0
Comment
Question by:APRESTOUS
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 2

Expert Comment

by:yaxh
ID: 19653886
// 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
 
LVL 11

Expert Comment

by:raj3060
ID: 19653893
try this:

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

You need to define what text you want to copy..
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 19653899
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Expert Comment

by:raj3060
ID: 19653920
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
 

Author Comment

by:APRESTOUS
ID: 19653941
This didn't work...

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

0
 

Author Comment

by:APRESTOUS
ID: 19653965
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
 
LVL 2

Expert Comment

by:yaxh
ID: 19654003
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
 
LVL 2

Accepted Solution

by:
yaxh earned 2000 total points
ID: 19654035
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question