Solved

Copy to clipboard javascript

Posted on 2004-03-25
8
2,555 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
ID: 10681669
<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
ID: 10681699
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
ID: 10681706
i'm not quite sure i follow your javascript with all the tables and stuff ..? i'm sure there's an easier way ?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 3

Expert Comment

by:btech1
ID: 10681733
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
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 200 total points
ID: 10681933
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
ID: 10682078
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
ID: 10689173
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
ID: 10815030
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

820 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