How to use 'this' in jQuery plugin callback?

javascript, jquery, this

Solution

You need to set the context for your callback. You can use `call`:

settings.callback.call(this);

Problem

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.

Original source