jQuery Plugin Template


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

This is a skeleton template for a jquery plugin with custom methods


Copy this code and paste it in your HTML
  1. /*
  2.  * jQuery Plugin Template
  3.  * Rickygri
  4.  */
  5. (function ($) {
  6. var defaults = {
  7. // total: 5
  8. };
  9. var options = $.extend(defaults, options);
  10. var o = options;
  11. var methods = {
  12. init: function () {
  13.  
  14. },
  15. fire: function () {
  16.  
  17. }
  18. };
  19. $.fn.popUp = function (methodOrOptions) {
  20. if (methods[methodOrOptions]) {
  21. return methods[methodOrOptions].apply(this, Array.prototype.slice.call(arguments, 1))
  22. } else if (typeof methodOrOptions === 'object' || !methodOrOptions) {
  23. return methods.init.apply(this, arguments)
  24. } else {
  25. $.error('Method ' + method + ' does not exist');
  26. }
  27. }
  28. })(jQuery);
  29.  
  30. // Use custom methods
  31. $(".elem").click(function() {
  32. $(this).popUp('fire');
  33. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.