4

I'm trying to bind zclip with live:

$('.code').live('click', function () {
    $(this).zclip({
        path: '<%= asset_path "ZeroClipboard.swf" %>',
        copy: $(this).text()
    });

    ...
});

It doesn't seem to work that way. Any clues?

I need to do live because some DOM elements are added with ajax.

Jacob
  • 6,317
  • 10
  • 40
  • 58

2 Answers2

1

You can put zclip-binding into the callback function in your ajax method, like this:

$.post('ajax',
   {data:"data"}, 
       function(data){
      //add dom elements
      ....
          //bind zclip
          $('.code').each(function(){
    $(this).zclip({
      path:".ZeroClipboard.swf",
      copy:$(this).txt()
        });
      });
   }, 'json');

each() is to avoid $('.code') have two or more elements.

0

Check the version of jQuery that you're using. You should be using .delegate() or .on()(if jQuery >= 1.7) instead anyways. (jQuery deprecated the .live method) They should work with your newly added elements via ajax.

$('.code').on('click', function () {
    $(this).zclip({
        path: '<%= asset_path "ZeroClipboard.swf" %>',
        copy: $(this).text()
    });
});
Caio Bianchi
  • 113
  • 4