Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this.find() work inside my plugin?

I'm trying to make a simple jQuery accordion plugin and can't figure out why doesn't the 'this' keyword work as it should. Here's the code of the plugin:

(function( $ ){
$.fn.accrdn = function(userSettings) {
    var defaults = { 
        toggle: true
    };
    var options = $.extend({}, defaults, userSettings);
    var handle = this.find('.handle');//doesn't work!

    if (options.toggle) {
        handle.click(function(){
            $(this).next('.panel').slideToggle();
        });
    } else {
        handle.click(function(){
            $(this).next('.panel').slideUp();
        });
    };
};
})( jQuery );
like image 404
user2240961 Avatar asked Jun 18 '26 02:06

user2240961


1 Answers

Now that the OP has provided an example fiddle of the code usage, the problem is the selector for the accordion div, it should be:

$(document).ready(function() {
    $('.accordion').accrdn({toggle:true, slideSpeed:500});
}); 

NOTE: The selector was missing the . to identify it is a class selector

Here is the fix

like image 165
musefan Avatar answered Jun 19 '26 18:06

musefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!