ZeroClipboard / zClip -- how to bind to live events?

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.

Answers


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.


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()
    });
});

Need Your Help

Shopify Styling

css sass shopify

Is there any way to get page identifier in css in shopify.

Bind RSS read cfc to a cftreeitem

coldfusion cfc

In CF8, I have a working cftree with an RSS feed per node (multiple nodes). The problem is that I am not currently using a bind which means that all the feeds are read when the page is loaded which...