Skip to content

Instantly share code, notes, and snippets.

@iconifyit
Created November 26, 2016 04:39
Show Gist options
  • Save iconifyit/6caa38e8e54ba3647744809a57a4ea54 to your computer and use it in GitHub Desktop.
Save iconifyit/6caa38e8e54ba3647744809a57a4ea54 to your computer and use it in GitHub Desktop.
A simple jQuery plugin example.
/**
* @author Scott Lewis
* @date June 4, 2011
* @link http://technify.me
*
* Just follow the comments in the code to understand what's going on.
*/
/**
* Create an anonymous function to avoid library conflicts
*/
(function($) {
/**
* Add our plugin to the jQuery.fn object
*/
$.fn.explainify = function(options) {
/**
* Define some default settings
*/
var defaults = {
"color": "#F00",
"font-weight": "bold"
};
/**
* Merge the runtime options with the default settings
*/
var options = $.extend({}, defaults, options);
/**
* Iterate through the collection of elements and
* return the object to preserve method chaining
*/
return this.each(function(i) {
/**
* Wrap the current element in an instance of jQuery
*/
var $this = $(this);
/**
* Do our kick-ass thing
*/
$this.css("color", options.color);
});
};
})(jQuery);
/**
* Calling the plugin
*/
$(function() {
$("h2").explainify({
"color": "blue",
"font-weight": "bold"
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment