View Full Version : javascript onclick copy to clipboard

08-25-2011, 09:32 PM

i have several links which i am trying to add an on click command to which copies a certain code to the clip board so the user can paste the code with out having to manually copy it first.

i have this

$latest = simplexml_load_file('codes_ending_soon.xml');
foreach ($latest->discount as $discount)
$code = $discount->id;
$link = $discount->link;
$store = $discount->storeName;
$logo = $discount->logo;
$details = $discount->details;
if($code_count <=4)
<div class="code_cont">
<div class="code_logo"><img src="<?php echo $logo?>" alt="<?php echo $store." code"?>" title="<?php echo $store." code"?>" height="32" /></div>
<a class="code" href="<?php echo $link?>" title="Click to see related product(s) / retailer and apply code" onclick="window.clipboardData.setData(<?php echo $code?>)"><?php echo $code?></a>
<div class="description"><?php echo $details?></div>

but when i click on the link nothing seems to copy :( at least in firefox anyway? can anyone help me please


08-26-2011, 12:49 AM
This solution is that when they click on the textarea it highlights the entire area. They simply right click to copy

<head runat="server">
<title>Untitled Page</title>

<script type="text/javascript">

function Select(ID)
var control = document.getElementById(ID);
var length = control.value.length;
if (control.createTextRange)
// IE
var range = control.createTextRange();
else if (control.setSelectionRange)
// FF
control.setSelectionRange(0, length);


<form id="form1" runat="server">
<textarea id="taCode" runat="server" cols="20" rows="10" readonly="readonly" style="overflow: scroll;
overflow-y: scroll; overflow-x: hidden; overflow: -moz-scrollbars-vertical;">This is the text that I want to be able to copy</textarea>