Skip to content

Instantly share code, notes, and snippets.

@Kcko
Created December 14, 2019 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/ea8559551e7aa171785aee9fe827df35 to your computer and use it in GitHub Desktop.
Save Kcko/ea8559551e7aa171785aee9fe827df35 to your computer and use it in GitHub Desktop.
// plugin example
(function($){
// custom select class
function CustomSelect(item, options) {
this.options = $.extend({
foo: 'bar'
}, options);
this.item = $(item);
this.init();
}
CustomSelect.prototype = {
init: function() {
this.item.css({opacity:0.5});
},
resetOpacity: function() {
this.setOpacity('');
},
setOpacity: function(opacity) {
this.item.css({opacity:opacity});
}
}
// jQuery plugin interface
$.fn.customSelect = function(opt) {
var args = Array.prototype.slice.call(arguments, 1);
return this.each(function() {
var item = $(this), instance = item.data('CustomSelect');
if(!instance) {
// create plugin instance if not created
item.data('CustomSelect', new CustomSelect(this, opt));
} else {
// otherwise check arguments for method call
if(typeof opt === 'string') {
instance[opt].apply(instance, args);
}
}
});
}
}(jQuery));
// plugin testing
$('select').customSelect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment