2

I'm using http://www.steamdev.com/zclip/#usage to copy some text to the clipboard and that code is working just fine. It uses flash to create a crossbrowser solution and it is based on ZeroClipboard, which seems to be considered to be the best working solution at the moment.

However I would like to have multiple copy to clipboard buttons or links on my page. Here is an example.

http://jsfiddle.net/stofke/TB23d/

This code works, it copies the text of the coupon code to the clipboard and opens up a new page with the correct link. How can I use that code on other links without having to duplicate it for each and every link / id.

Using just the class

$(function() {
$('.copy').zclip({
    path: 'http://shopsheep.com/js/ZeroClipboard.swf',
    copy: $(this).text(),
    afterCopy: function() {
        window.open($(this).attr('href'));
    }
});

});

doesn't work: as you can see here: http://jsfiddle.net/stofke/EAZYW/ if you remove the afterCopy function you'll see that $(this).text() will return the whole page instead of just the text between the link tag.

doing something like this

$(function() {
$('a.copy', this).zclip({
    path: 'http://shopsheep.com/js/ZeroClipboard.swf',
    copy: $('a.copy', this).text(),

});

});

slightly improves upon it but returns all text between the link tag as you can see here. http://jsfiddle.net/stofke/hAh3j/

Stofke
  • 2,928
  • 2
  • 21
  • 28
  • I’m voting to close and delete this question because the code on which it is based no longer exists. I cannot delete my own answer – mplungjan May 12 '20 at 06:28

4 Answers4

3

UPDATE: This no longer works but I cannot delete the post

This seems to work - someone might be able to make it more elegant

http://jsfiddle.net/5nLw6/7/

$(function() {
    $('.copy').each(function() {
        var linkId = $(this).attr("id");
        $(this).zclip({
        path: 'http://shopsheep.com/js/ZeroClipboard.swf',
        copy: $("#"+linkId).text(),
        afterCopy: function() {
            window.open($('#'+linkId).attr('href'));
        }
    });
  });
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    I tried that but it isn't working. Try forking the jsfiddle with this code and you'll see it's not working. http://jsfiddle.net/stofke/EAZYW/ – Stofke Mar 31 '11 at 06:55
  • I tried zClip within a table, it fails, no Flash is rendered. Going to use ZeroClipboard directly now. – basZero Feb 01 '13 at 15:54
  • To whoever decided to downvote a two year old post without leaving a comment... Thanks. Love the fly-by downvotes. – mplungjan Mar 27 '13 at 20:58
  • @Jota This is a NINE YEAR OLD post. Things have changed. The SWF is no longer there and if it were, it would likely be blocked. Instead of voiting down on a NINE YEAR OLD POST, just post a comment saying this no longer works – mplungjan May 12 '20 at 06:21
2

I actually discovered that using ZeroClipboard directly is just as easy, I just added this code in case someone wants a solution without using zclip.

ZeroClipboard.setMoviePath('http://shopsheep.com/js/ZeroClipboard.swf');
$(document).ready(function() {
    $(".copy").each(function(i) {
        var clip = new ZeroClipboard.Client();
        var myTextToCopy = $(this).text();
        var myTextUrl = $(this).attr('href');
        clip.setText(myTextToCopy);
        clip.addEventListener('complete', function(client, text) {
            window.open(myTextUrl);
        });
        clip.glue($(this).attr("id"));
    });
});

http://jsfiddle.net/stofke/JxMbd/

Stofke
  • 2,928
  • 2
  • 21
  • 28
0

This is what we follow in Oodles Technologies.

To use zero copy to clipboard you need two files 1 . ZeroClipboard.js
2 .ZeroClipboard.swf both file can be download from here

<html>
<head>
    <script src =”../ZeroClipboard.js”></script>
    <script >
     // configure ZeroClipboard first
     ZeroClipboard.config( { moviePath : /path/swffile/ZeroClipboard.swf } );

     // initialize constructor
    var client = new ZeroClipboard($(“#elementid”));

    /* elementid is the element on which click , the data will copy  to clipboard. you can also pass multiple elements, it use jquery selector */
        </script>
<body>
<input type=”text”  id =”targetid”></button>
<button  id =”elementid” data-clipboard-text ='data for copy’ >copy</button>
</body>
</head>
<html>

ZeroClipboard automatically copy the value of data-clipboard-text attribute when event accur on element pass to ZeroClipboard's constructor

0

Light weight jQuery solution... re-use class to copy text from any element.

$(document).on('click', '.copytoclipboard', function(e) {
  if($("#holdtext").length < 1)
    $("body").append('<textarea id="holdtext" style="height:0;width:0;border:0;outline:0;resize:none;"></textarea>');
  $("#holdtext").val($(this).text()).select();
  document.execCommand("Copy");
});
Dazza
  • 126
  • 7