Use Javascript to copy text from label

Label1 (asp.net control) is inside Panel1 of my webpage and I have a bt button. What is Javascript for copying text from Label1 to the clipboard?

@artlung, I have placed below code just outside my form but inside the body. The last line of code I put inside Panel1 of my form. There is something 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>

      

+2


a source to share


2 answers


Use the zeroclipboard library .



+1


a source


I have achieved this with javascript. I figured out that javascript renamed the label from Label1 to MainContent_Label1. Then I also saw through debugging that what I wanted from the variable was the innerText property. Problem solved!

I also had a function test for an empty string and then return confirmation to the user when either copied successfully to the clipboard or when the shortcut is empty and nothing needs to be copied. This is all the client side.



    <asp:Label ID="Label1" runat="server" Text="Copy This!!!"></asp:Label>

  <script type="text/javascript">
      function ClipBoardTest() {
          var txt = document.getElementById('MainContent_Label1');
          window.clipboardData.setData("Text", txt.innerText);
          if(!txt){
              alert("Nothing to Copy");
              }
          else {
              alert("Copy to ClipBoard Successful!");
              }
      } 
    </script> 

    <input type="button" id='bt' onclick="ClipBoardTest();" value="Copy" />

      

+2


a source







All Articles