Adding a callback handler to a jQuery plugin
When you're creating a plugin sometimes you'll have to deal with callbacks, and there is a lot of ways to do it, and here I show you one of these :
(function($){
$.fn.myAwesomePlugin = function(settings) {
var callback = settings.callback;
if ($.isFunction(callback)) {
var parameter = 'Hello World';
callback.call(this, parameter);
}
};
})(jQuery);
And then when you call your plugin, it would be looking like this:
$("#a-random-element").myAwesomePlugin({
callback: function(data){
alert(data);
}
});Written by Eric Martins
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Jquery
Authors
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#