Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Plugin: Handling Events

I'm currently working on a jQuery plugin and I'm wondering if it would be possible to have the plugin listen for events rather than being triggered by events.

So instead of this:

$('#element_id').mouseover(function() {
    $(this).plugin();
});

$('#element_id').mouseout(function() {
    $(this).pluginHide();
});

I want to try to do something like this:

$('#element_id').plugin(????, ????);

And replace the ?'s with some sort of reference to the element and event that should trigger it to show or hide. I'm still getting my feet wet with jQuery, so bear with me if a solution to this is painfully obvious.

like image 842
Wilco Avatar asked Nov 25 '08 23:11

Wilco


1 Answers

After experimenting a bit, it looks like I was looking for the built-in "bind()" function. To solve my original problem:

jQuery.fn.plugin = function(show, hide) {
    this.bind(show, function() {
        // Do something to show.
    };

    this.bind(hide, function() {
        // Do something to hide.
    };
};
like image 147
Wilco Avatar answered Sep 28 '22 04:09

Wilco