Click to copy Hidden Field

Y

Yoni

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

Yoni

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
 
E

Evertjan.

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;
 
V

VK

Yoni said:
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>

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top