Solved

Copy to clipboard javascript

Posted on 2004-03-25
8
2,533 Views
Last Modified: 2012-06-21
Hi everyone,

i'm stuck on something that i've searched the web for, but the scripts out there either don't work or are not what i need..

I need to have an image (link) copy the contents of a hidden span or input field into the clipboard.

so in other words my page will have a hidden field containing some data, and the user clicks a little image to copy that data to their clipboard, nothing fancy, doesnt even have to work in all browsers, just IE.

Thanks in advance

Chris
0
Comment
Question by:garfield1979
8 Comments
 
LVL 3

Expert Comment

by:btech1
Comment Utility
<html>
<head>
<script>
function doIt() {
     var x=document.getElementById('dajm');    
     document.form.copyArea.value=x.innerHTML;
     alert(document.form.copyArea.value);
     y = document.form.copyArea.createTextRange();
     y.select();
     y.execCommand("Copy");
}
</script>
</head>

<body onload="doIt()">

<p>Outside Div</p>
<div id="dajm">
  <table border="1" width="100%">
    <tr>
      <td width="50%">Inside Div</td>
      <td width="50%">&nbsp;</td>
    </tr>
    <tr>
      <td width="50%">&nbsp;</td>
      <td width="50%">&nbsp;</td>
    </tr>
  </table>
</div>
<form name="form">
  <input type="hidden" name="copyArea" value>
</form>
<p>Outside Div</p>
</body>
</html>
0
 
LVL 3

Expert Comment

by:btech1
Comment Utility
Forget the above, try this instead...

===========

<html>

<head>
<script>
function doIt() {
     var x=document.getElementById('dajm');    
     document.form.copyArea.value=x.innerText;
     y = document.form.copyArea.createTextRange();
     y.select();
     y.execCommand("Copy");
}
</script>
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
</head>

<body>

<p>Outside Div</p>
<div id="dajm">
  Inside Div Content
</div>
<form name="form">
  <input type="hidden" name="copyArea" value><input type="button" value="Copy to Clipboard" onclick="doIt()">
</form>
<p>Outside Div</p>

</body>

</html>
0
 

Author Comment

by:garfield1979
Comment Utility
i'm not quite sure i follow your javascript with all the tables and stuff ..? i'm sure there's an easier way ?
0
 
LVL 3

Expert Comment

by:btech1
Comment Utility
I just want to show you it is only copying the text within the div, and not the whole page, which means you only need the doIt() script and the onclick="doIt()" to run
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 31

Accepted Solution

by:
GwynforWeb earned 200 total points
Comment Utility
try this

<form>
<INPUT TYPE="button" VALUE="copy" onClick="hid.value=document.getElementById('divName').innerHTML;hid.createTextRange().execCommand('Copy')">
<input type=hidden name=hid>
</form>

<div id='divName'>
hello world copy me!!
</div>
0
 

Author Comment

by:garfield1979
Comment Utility
hey thanks Gwyn, that worked, for some reason if i separate the other scripts and put them in the head, then they dont work, i have a page full of ASP and i think something isnt' working from the head to where the button is placed...

It works with a button, now how can i make it work with an image, if you click the image it'll copy to the clipboard ?
0
 

Author Comment

by:garfield1979
Comment Utility
Can anyone help me make Gwyns solution work in a different way ? again i need to put that code in an image instead of a button with the same effect.

Thanks in advance
0
 
LVL 3

Expert Comment

by:zastil
Comment Utility
yes

<form>
<INPUT TYPE="image" src="myimage.gif" VALUE="copy" onClick="hid.value=document.getElementById('divName').innerHTML;hid.createTextRange().execCommand('Copy')">
<input type=hidden name=hid>
</form>

<div id='divName'>
hello world copy me!!
</div>
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now