Solved

Copy to clipboard javascript

Posted on 2004-03-25
8
2,565 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Keyup listener getting fired on initial page load 1 49
Call a VB.net function in Javascript 6 49
allow link tag to be submit like form 15 58
Multiple of Image Swaps 5 41
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

738 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