Solved

Copy to clipboard javascript

Posted on 2004-03-25
8
2,571 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

688 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