jQuery plugin starter code


/ Published in: jQuery
Save to your folder(s)

Basic skeleton for a plugin. Contains an example to show where code needs to go.


Copy this code and paste it in your HTML
  1. (function($){
  2.  
  3. $.fn.pluginName = function(params){
  4.  
  5. // give the options their default value
  6. options = $.extend({
  7.  
  8. option1: 'default text' // example option (delete)
  9.  
  10. }, params);
  11.  
  12. // plugin actions. Most code should go here.
  13. this.each(function(){
  14.  
  15. $(this).fadeOut('slow', function(){ $(this).html(options.option1).fadeIn() }); //example action (delete and replace with your code)
  16.  
  17. });
  18.  
  19. return this; // keeps jquery chaining going
  20.  
  21. }
  22.  
  23. })(jQuery);
  24.  
  25. $('p').pluginName({option1:'new text'}); // call the plugin, pass it the options

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.