views:

115

answers:

1

Hello All-

Label1 (asp.net control) is located inside Panel1 of my webpage and I have a button called bt. What is the Javascript to copy the Text from Label1 to the clipboard?

Thanks,

@ artlung, I placed the below code just outside of my form but inside the body. The last line of code I placed inside Panel1 of my form. Anything wrong with this code because nothing happens when I click the Copy to Clipboard button.

<script language="JavaScript"> 
                    var clip = new ZeroClipboard.Client(); 
                    clip.addEventListener( 'mouseDown', function(client) {  
                            // set text to copy here 
                            clip.setText( document.getElementById('form1.Label1').value ); 

                            // alert("mouse down");  
                    } ); 

                    clip.glue( 'd_clip_button' ); 
            </script> 

The next line of code is above the script tags but inside Panel1 in my form

<div id="d_clip_button">Copy To Clipboard</div>
+1  A: 

Use the zeroclipboard library.

artlung
isn't there some simple javascript that can accomplish this?
Josh
If there were, libraries such as zeroclipboard would not exist.
artlung
So following the instructions, if I set var clip = new ZeroClipboard.Client(); do you know how I can set clip to copy Label1.Text? I was going to use a button called bt and on its onclick event set it to the zeroclipboard somehow but I don't know the syntax
Josh
Yes, that is correct. Read the instructions carefully: http://code.google.com/p/zeroclipboard/wiki/Instructions you'll be calling `setText()` for the button. It depends on what the button id is, what other libraries you might have included.
artlung
so on my button onclick="setText()" ? Then in my Javascript tag I just need to set var clip = new ZeroClipboard.Client(); and clip.setText('Label1.Text'); ? This doesn't seem to work and have tried many different ways. Do you have the correct syntax for this?
Josh
No, you can't just call setText() like that. You REALLY need to read the instructions. Failing that, have a look at the examples provided: http://code.google.com/p/zeroclipboard/wiki/Instructions#Minimal_Example and http://code.google.com/p/zeroclipboard/wiki/Instructions#Complete_Example
artlung
I DID read the instructions... I'm sorry if I overlooked anything but I'm just asking for some simple syntax help. THANKS..
Josh
So what errors are you getting? Use Firefox and the JavaScript console and report back the errors.
artlung
I pasted my code at the top as that is the only place I knew to paste it. Do you see any problems with it?
Josh
this worked. thanks artlung clip.setText( document.getElementById("Label1").innerHTML );
Josh
@Josh, great! I often find it frustrating implementing someone else's library. Way to stick to it!
artlung