Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8954785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:23:22+00:00 2026-06-15T14:23:22+00:00

I am want to build a plugin with accessible methods and options, this for

  • 0

I am want to build a plugin with accessible methods and options, this for a complex plugin.
I need the methods to be accessible outside the plugin because if somebody ads something to the DOM it needs to be updated(so we dont need the run the complete plugin again).

I have seen in the past that there are plugin that do it like this, but I cant find them, so I cant take a look at them. I am still new to javascript so any help would be nice.

It would be nice if we still can globally override the options.

How I want to use the plugin:

// options
$('#someid').myplugin({name: 'hello world'});

// methods(would be nice if we can use this)
$('#someid').myplugin('update');

// my old plugin wrapper

;(function($, window, document, undefined){

    $.fn.pluginmyPlugin = function(options) { 

        options = $.extend({}, $.fn.pluginmyPlugin.options, options); 

            return this.each(function() {  

                var obj = $(this);

                // the code 
            });     
        };

        /**
        * Default settings(dont change).
        * You can globally override these options
        * by using $.fn.pluginName.key = 'value';
        **/
        $.fn.pluginmyPlugin.options = {
            name: '',
                            ...         
        };

})(jQuery, window, document);

Update

So after looking at the jQuery docs I have build the following code, please let me know if there’s something wrong with the code, if it can be build better…

;(function($, window, document, undefined){

    var methods = {

        init : function( options ) {

            options = $.extend({}, $.fn.pluginmyPlugin.options, options); 

            return this.each(function(){

            alert('yes i am the main code')

            });
        },
        update : function( ) {
             alert('updated')
        }
    };

    $.fn.pluginmyPlugin = function(method) { 

        if ( methods[method] ) {
          return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
          return methods.init.apply( this, arguments );
        } else {
          $.error( 'Method ' +  method + ' does not exist on this plugin' );
        }    

    };

        /**
        * Default settings(dont change).
        * You can globally override these options
        * by using $.fn.pluginName.key = 'value';
        **/
        $.fn.pluginmyPlugin.options = {
            name: 'john doe',
            //....
        };

})(jQuery, window, document);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:23:23+00:00Added an answer on June 15, 2026 at 2:23 pm

    An alternative:

    var Plugin = function($self, options) {
      this.$self = $self;
      this.options = $.extend({}, $.fn.plugin.defaults, options);
    };
    
    Plugin.prototype.display = function(){
      console.debug("Plugin.display");
    };
    
    Plugin.prototype.update = function() {
      console.debug("Plugin.update");
    };
    
    $.fn.plugin = function(option) {
      var options = typeof option == "object" && option;
    
      return this.each(function() {
        var $this = $(this);
        var $plugin = $this.data("plugin");
    
        if(!$plugin) {
          $plugin = new Plugin($this, options);
          $this.data("plugin", $plugin);
        }
    
        if (typeof option == 'string') {
          $plugin[option]();
        } else {
          $plugin.display();
        }
      });
    };
    
    $.fn.plugin.defaults = {
      propname: "propdefault"
    };
    

    Usage:

    $("span").plugin({
      propname: "propvalue"
    });
    

    $("span").plugin("update");
    

    This absurdly resembles the Twitter Bootstrap’s JavaScript template. But, it wasn’t completely taking from there. I have a long history of using .data().

    Don’t forget to wrap it appropriately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On a grails progect I want to build a binary plugin with hook implementable
I want build a sketch pad app on iPhone, I assume that this type
I want to build a VST plugin on OSX. I can compile it just
Ok so i want to build an Excel plugin (custom task pane) that'll act
I apologize if this question is vague, but I want to build a drop
Looking for an attractive, highly customizable forum plugin to implement. Don't want to build
I want to build a plugin for an application(TeamSpeak), however I have no lua
I want to build a cuda plugin for an other project written in C++
I want to build a simple jQuery plugin, but I don't know how to
I am facultatively commenting out the sbt-gpg-plugin in ~/.sbt/plugin/build.sbt , because I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.