2

I have 2 pre blocks, each of them is wrapped with a div and has a copy button.

<div class="code">
    <a class="copy">copy</a>
    <pre>content of 1st pre</pre>
</div>

<div class="code">
    <a class="copy">copy</a>
    <pre>content of 2nd pre</pre>
</div>
$('.code').on('mouseenter', function() {
    var copy_button = $(this).find('.copy');
    var clip = new ZeroClipboard(copy_button, {moviePath: 'ZeroClipboard.swf'});
    var content = $(this).find('pre').text();

    // at this point, content is always right
    // alert(content);

    clip.on('mousedown', function(client, args) {
        // the content doesn't get updated here
        alert(content);

        clip.setText(content);
    });
});

The problem is, it seems that it always copys the conent of the first-mouseentered-div.

Say I first mouseentered div2, and clicked copy, the content (content of 2nd pre) is copied fine. But then when I try to copy the first pre, the content doesn't get updated, it's still content of 2nd pre.

What am I doing wrong here? How can I fix this?

user1643156
  • 4,407
  • 10
  • 36
  • 59

2 Answers2

0

OK, I found another jQuery plugin - zClip, which is built using the Zero Clipboard library. It's much easier to use and configure.

$('.copy').zclip({
    path: 'ZeroClipboard.swf',
    copy: function() {
        var tocopy = $(this).parent().find('pre').text();
        // formatting content
        // ...
        return tocopy;
    },
    beforeCopy:function(){
        // do something before copy
    },
    afterCopy:function(){
        // do something after copy
    }
});
user1643156
  • 4,407
  • 10
  • 36
  • 59
0

You keep on adding more and more events on mouse enter. This should unbind events so you don't add each time:

.on('mouseout', function(){
    $(this).unbind();
});
Jason Prawn
  • 1,003
  • 11
  • 20
  • ok, that is only related to div.code. it doesn't even matter if I unbind or not. that has nothing to do with the copied-content problem. – user1643156 Jan 28 '13 at 16:39
  • Jeepers. Just can't get that mousedown event to fire. Try adding a function round it: (function(content){ })(content); to protect the variable. – Jason Prawn Jan 28 '13 at 17:34