Try this
Plugin
(function($) {
$.fn.myPlugin = function(options) {
var settings = $.extend({
// Default options
onComplete: null, // Callback for completion
onClick: null // Callback for a click event
}, options);
// Plugin logic
return this.each(function() {
var $this = $(this);
// Example: Trigger onComplete callback
if (typeof settings.onComplete === 'function') {
settings.onComplete.call($this); // Call with 'this' context
}
// Example: Trigger onClick callback on a click event
$this.on('click', function() {
if (typeof settings.onClick === 'function') {
settings.onClick.call($this, event); // Pass event object
}
});
});
};
})(jQuery);
Call
// Usage example:
$('#myElement').myPlugin({
onComplete: function() {
console.log('Plugin completed on:', this); // 'this' refers to #myElement
},
onClick: function(event) {
console.log('Element clicked:', this, 'Event:', event);
}
});