JSFiddle - React, Tailwind, and code Playground
by Osman Salihovic
HTML
<script src="https://code.jquery.com/jquery-2.2.3.js"></script>
<div id="elem" data-plugin-options='{"message":"Goodbye World!"}'></div>
JavaScript
(function(window, $){
var Plugin = function(elem, options){
this.elem = elem;
this.$elem = $(elem);
this.options = options;
this.metadata = this.$elem.data('plugin-options');
};
Plugin.prototype = {
defaults: {
message: 'Hello world!'
},
init: function() {
this.config = $.extend({}, this.defaults, this.options, this.metadata);
this.displayMessage();
return this;
},
displayMessage: function() {
alert(this.config.message);
}
}
Plugin.defaults = Plugin.prototype.defaults;
$.fn.plugin = function(options) {
return this.each(function() {
new Plugin(this, options).init();
});
};
window.Plugin = Plugin;
})(window, jQuery);
//Set the message per instance:
$('#elem').plugin({
message: 'Goodbye World!'
});
var p = new Plugin(document.getElementById('elem'), {
message: 'Goodbye World!'
}).init();
//Or, set the global default message:
Plugin.defaults.message = 'Goodbye World!';