jQuery: Trigger help

292 views Asked by At

I'm using the cluetip jQuery plugin.

I'm trying to add my own close button. The the jquery I'm trying to call is:

 $(document).bind('hideCluetip', function(e) {
  cluetipClose();
});

There are many references to cluetipClose() through the code and the button that the jquery inserts uses it and works so that function as far as I'm aware works fine.

I'm trying to trigger that using

$('a.close-cluetip').trigger('hideCluetip');

I've created my link:

<a href="#" class="close-cluetip">Close</a>

But it isn't doing anything.

Am I calling it incorrectly?

1

There are 1 answers

2
treeface On

The problem here is that in the cluetip plugin, the function clueTipClose() is inside a closure, so you have no access to it unless you're inside the closure (i.e. inside the plugin's code). Now I've gotta admit, this plugin doesn't seem to be set up to be all that extensible. If they made this function accessible via a "clueTip" object that was set up for each element that uses it, you'd be able to add another jQuery method to the end of the closure like this:

$.fn.cluetipClose = function() {
    return this.each(function() {
        var thisCluetip = findCluetipObj(this);

        if (thisCluetip)
            thisCluetip.cluetipClose();
    });
};

But you have the unfortunate luck of not being able to do this easily. It looks like this guy wrote his jQuery plugin with non-OO code inside of a closure. Poor you.

Now on the plus side, it seems this plugin is already running this code directly after it instantiates the cluetipClose() function. Have you tried just doing this from your code:

$('a.close-cluetip').trigger('hideCluetip');

Without redeclaring the document hideCluetip bind? I think that should probably work.