How to use 'this' in jQuery plugin callback?

Posted by Bondye on Stack Overflow See other posts from Stack Overflow or by Bondye
Published on 2013-07-02T10:53:48Z Indexed on 2013/07/02 11:05 UTC
Read the original article Hit count: 184

Filed under:
|
|

At the moment I am creating a jQuery plugin.

I am facing a problem when using a callback.

Plugin code

$.fn.bondye = function (options) {
    var settings = $.extend({
        callback: function () {}
    }, options);

    return this.each(function () {
        $(this).addClass('test');
        settings.callback();
    });
};

How I want to execute the code

$(".myDiv").bondye({
    callback: function () {
        $(this).removeClass("test");
    }
});

jsFiddle example: http://jsfiddle.net/HEJtm/1/

But I aint able to do anything with the div within the callback.

I used http://learn.jquery.com/plugins/advanced-plugin-concepts/ to learn developing jQuery plugins, but I cannot find anything about callbacks.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery