ZeroClipboard / zClip - how to get attached to live events?

I am trying to link zclip to live:

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

This seems to be not the case. Any clues?

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

+4
source share
2 answers

You can put the zclip binding in a callback function in your ajax method, for example:

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

Each () should avoid $ ('. Code') having two or more elements.

+1
source

Check out the jQuery version you are using. You should use .delegate () or .on () (if jQuery> = 1.7), anyway. (jQuery did not recommend the .live method) They should work with your added elements via ajax.

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

Source: https://habr.com/ru/post/1385550/


All Articles