/ Published in: JavaScript
This is a really basic skeleton template for jQuery plugins. It provides a callback function which can be called anywhere in your plugin with base.success(); This will return the current element, but you can pass anything you like back by changing the base.$el value passed to the function to whatever you like.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* Plugin code */ (function($){ $.pluginName = function(el, option, callback){ /* Setup base elem vars */ var base = this; base.$el = $(el); base.el = el; base.$el.data("pluginName", base); /* Option object */ base.o = option; /* * Init */ base.init = function () { }; /* * Callback func when successful (returns element) */ base.success = function() { callback(base.$el); // Return elem }; base.init(); }; $.fn.pluginName = function(option, callback){ return this.each(function(){ (new $.pluginName(this, option, callback)); }); }; })(jQuery); /* Use plugin */ $(document).ready(function () { $(document).pluginName(100, function(elem) { // Success console.log("Success"); }); });