Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Click to copy Hidden Field

2,370 views
Skip to first unread message

Yoni

unread,
Oct 14, 2009, 5:23:42 PM10/14/09
to
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. Any
Ideas.There are 2 example ones that works (non-Hidden) and one that
doesnt (Hidden). I don't need both to work only the hidden one to
start working.

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>

Thomas 'PointedEars' Lahn

unread,
Oct 14, 2009, 5:58:43 PM10/14/09
to
Yoni wrote:

> 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

Stefan Weiss

unread,
Oct 14, 2009, 6:21:44 PM10/14/09
to

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

Yoni

unread,
Oct 15, 2009, 2:04:38 PM10/15/09
to
Hey Thanks so much. I really am interested in getting a good script
that works cross platform. Do you know of any? If not thanks at least
for fixing my issue and Ill use it till I can find a better script.

Thank you
Yoni

Evertjan.

unread,
Oct 15, 2009, 3:16:29 PM10/15/09
to
Yoni wrote on 15 okt 2009 in comp.lang.javascript:

> 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)

VK

unread,
Oct 15, 2009, 3:52:13 PM10/15/09
to

<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.

0 new messages