I have an external script, which I can't modify. This script load a <a>
button, and add a jQuery .click on it... and it finish with "return false".
I need to trigger my own code on this click. When I load the page the <a>
doesn't exist, so I need to use .on('click')
to bind "live". But it looks like the .on('click')
is loaded "after" the .click and as he use "return false", my .on('click')
is not loaded.
So the question is... How can I trigger my on click on this dynamically loaded a#btn which already has a .click function returning false?
Here is the fiddle :
http://jsfiddle.net/PLpqU/
And here a code example :
<div id="container"></div>
// I want this action to be executed on click, and the other too
// I can't use .click because on the "real" code, the a#btn is loaded after the page by another script
jQuery(document).on('click','a#btn',function(){
ga('send', 'event', { eventCategory: 'xxxx', eventAction: 'yyyy' });
}) ;
// http://www.xxxxxx.com/distant-script.js
// This one is binded in a script that I cannot edit :
// Just before it load a#btn on the page, and then bind .click on it
// as he return false, the other on('click') are not executed
jQuery('#container').append('<a id="btn" />') ;
jQuery('a#btn').click(function(){
// Some code stuff I need to be executed, but I can't manage
return false ;
}) ;
As you can the, the objective is to trigger a Google Analytics event on a link button loaded by a distant script.