Thanks in advanced.
<head>
<script>
function copyToClipboard(field)
{
var content = eval("document."+field)
content.focus()
content.select()
range = content.createTextRange()
range.execCommand("Copy")
window.status="Contents copied to clipboard"
setTimeout("window.status=''",1800)
}
</script>
</head>
<body>
<form name=myform1>
<input type=hidden name=mytext1 value='This Hidden Field Wont Copy'></
form>
<a href="#" onclick="javascript:copyToClipboard('myform1.mytext1')"
value="Send to Clipboard">1<a/>
<form name=myform2>
<input type=text name=mytext2 value='This non hidden field will
copy.'>
<a href="#" onclick="javascript:copyToClipboard('myform2.mytext2')"
value="Send to Clipboard">2<a/>
</form>
</body>
> Hey All, Ive been looking for hours trying to search for a fix for
> this. I want to click to copy a Hidden Field. I have the following
> script but when hidding the field it says it wont work.
No, "it" doesn't say that.
<http://jibbering.com/faq/#posting>
PointedEars
It "does not work" because you can't focus a hidden form field.
Surprisingly enough, this is exactly what the error message in IE says.
There is no need to focus or select anything here, so you can just drop
the focus() and select() lines, and your script will "work" (for low
values of work). createTextRange and execCommand are only supported by a
subset of browsers, which means that your script won't run in Firefox,
Opera, etc.
There are so many other things wrong with this script that I'm not going
to correct anything else, unless you're really interested. You could
start by reading the group FAQ; section 6.2, for example, tells you how
to access form fields without eval().
cheers,
stefan
Thank you
Yoni
> I really am interested in getting a good script
> that works cross platform. Do you know of any?
Yes I do:
var x = 1 * 3;
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
<head>
<script>
function copyToClipboard(txt) {
if (window.clipboardData && clipboardData.setData) {
clipboardData.setData("Text", txt);
}
else {
/* not IE */
}
}
</script>
</head>
<body>
<form name=myform1>
<input type=hidden name=mytext1
value='This Hidden Field Does Copy Now In IE'>
</form>
<p><a href="#" onclick="
copyToClipboard(
document.forms['myform1'].
elements['mytext1'].value
);
return false;
">Send to Clipboard</a></p>
</body>
</html>
On the majority of other browsers scripted clipboard manipulations are
rightly considered dangerous so they either require extended
privileges over script signing (Gecko browsers) or simply not allowed
at all.
If you care about IE only, then this is the solution right above, you
don't need to bother with textRange. If you need a universal solution
than first drop the current approach as not doable. As the global
rule: unless user does come real mechanical actions to copy/past some
content, you have no means to enforce it on him - yet you may prompt
him for actions.