Copy text to clipboard with jquery or javascript

how to copy some text to clipboard with jquery or javascript, from google. i know there

is a pludgin named zeroclipboard http://code.google.com/p/zeroclipboard/ can do this with

crossbrowsers. http://code.google.com/p/zeroclipboard/wiki/Instructions instructions

but when I tested it on my site. I set it to copy text. he cannot work.

my test link is http://xanlz.com/copy/1.html

it always copies all values. even uncheck the box. maybe the value does not change. but when i am a alter()variable, the value is ok. how to fix? thank.

I want it to be able to copy the value of the checkbox. if the check box is not selected, then do not copy its value.

+3
source share
2 answers

EDIT:

It took a little time to figure this out (I didn’t have the right links to .swf), but here is a complete working example (IE 8 and Firefox 4 checked)

<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
    <script type="text/javascript" src="http://zeroclipboard.googlecode.com/svn/trunk/ZeroClipboard.js" ></script>
    <script type="text/javascript" >
        $(document).ready(function(){
            var checkall = $('#checkall');
            var boxes = $('input[type="checkbox"]').not(checkall);
            checkall.click(function () {
                boxes.attr('checked', this.checked);
            });
            boxes.change(function() {
                checkall[0].checked = this.checked && boxes.filter(':checked').length === boxes.length;
            });

            ZeroClipboard.setMoviePath('http://zeroclipboard.googlecode.com/svn-history/r10/trunk/ZeroClipboard.swf');
            var clip = new ZeroClipboard.Client();

            clip.glue("copyvalue");
            clip.addEventListener( 'onMouseOver', function(){
                var text = ' '; //Make this a space since we can't copy nothing...
                $('input.forminput:checked').each(function() {
                    text += $(this).val() + "\n";
                });
                clip.setText(text);
            });
        }) ;
    </script>
  </head>
  <body>
    <input class="forminput" type="checkbox" value="test one" checked="checked" name="VD1">
    <br>
    <input class="forminput" type="checkbox" value="test two" checked="checked" name="VD2">
    <br>
    <input class="forminput" type="checkbox" value="test three" checked="checked" name="VD3">
    <br>
    <input class="forminput" type="checkbox" value="test four" checked="checked" name="VD4">
    <br>
    <input class="forminput" type="checkbox" value="test five" checked="checked" name="VD5">
    <br>
    <input id="checkall" type="checkbox" checked="checked" name="checkall">
    <input id="copyvalue" class="button" type="button" value="copy test">
  </body>
</html>   

ORIGINAL:

You do not have a click event for your button.

You need to add something like:

    var clip = new ZeroClipboard.Client();
    $("#copyvalue").click(function(){
        var text = '';
        $('input.forminput:checked').each(function() {
            text += $(this).val() + "\n";
        });
        //alert(text);
        clip.setText(text);
    });
+1
source

If you do not like to use (or are not allowed) to rely on flash (which I personally won’t), I think you better create a text area with text (or serialized data) pre-selected using the instructions for the user to copy and paste the data .

, , (, ctrl + c windows/linux ⌘-C mac).

, , ...

0

All Articles