dimanche 3 mai 2015

jQuery add $(this) to callback

How can I add $(this) to the following code in the callback? I don't have access to $(this) in the callback function if i simply try adding it.

$('.example').myPlugin({
    option1: hello, 
    option2: world,
    callback: function () {
      // use $(this)
    }
 });

Edit: How can I add for example var $this = $(this) inside $('.example').myPlugin({... }) before the callback?

Part of plugin with callback:

var base = this;

// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;

base.pluginName = function () {
...
// Callback
if (typeof base.options.callback === 'function') {
  base.options.callback.call(el);
}
...
}

Aucun commentaire:

Enregistrer un commentaire