jQuery Plugin Boilerplate Codes

If you’ve just started writing your fist plugin, here is the most simple boilerplate for a jQuery plugin.


(function ( $ ) {
    $.fn.myPlugin = function(options) {    	

    	//default settings
    	var defaults = {            
            option1: "",             
            option2: 200, 
    	var settings = $.extend({}, defaults, options );	    

        return this.each(function(){
    		var element = $(this);
    		//do something to the element here
}( jQuery ));

How to use the plugin

$('#any-id').myPlugn({option1: 'hello', option2: 1000});

Leave a Comment

Your email address will not be published.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.