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 8740283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:06:13+00:00 2026-06-13T11:06:13+00:00

I’m rewriting a plugin so that each main section is turned into a method

  • 0

I’m rewriting a plugin so that each main section is turned into a method as per this question and the jQuery documentation, and so far the whole thing is still working.

The plugin is initialized with:

$('#mySelector').myPlugin({
    // want to keep initial options
    option1: true,
    option2: 'right'
    // etc.
});

Then later, I want to use a refresh method:

$('#mySelector').myPlugin('refresh');

Basically, I need “refresh” to do everything in this plugin, except re-applying the HTML DOM modifications (while keeping all of the original initialized options).

I can’t seem to figure it out. I cannot just use methods.binders(var1, var2); because I need to get the variables too. However, the init() needs the methods.html() when it’s first initialized. How can I skip that one part when I do my refresh?

Maybe it’s simple, but I have a headache now.

(function ($) {

    var methods = {
        defaults : {
            option1: false,
            option2: 'left'
                    // etc.
        },
        settings : {},
        init : function(options) {
            methods.settings = $.extend({}, methods.defaults, options);

            $(this).each(function() {
                if ($(this).is('[type=radio]')) {
                    var var1 = $(this);
                    var var2 = $('.somethingelse');
                    // etc.
                    methods.html(var1, var2);
                    methods.binders(var1, var2);
                };
            });

        },
        refresh : function () {
            // no idea??
        },
        html : function(var1, var2) {
            // various HTML DOM manipulations
        },
        binders : function(var1, var2) {
            // binding various events, hover, click, etc.
            // assigns classes
        }
    };

    $.fn.myPlugin = 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 jQuery.myPlugin' );
        }
    };

})(jQuery);
  • 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-13T11:06:14+00:00Added an answer on June 13, 2026 at 11:06 am

    Following moves most of the code from methods.init to a function doInit() that can be called in both methods.init and methods.refresh with argument for initType and a conditional inside to call some code for init but not refresh

    (function($) {    
    
        var methods = {     
            /* see answer edit about moving this out of here*/        
            defaults: {
                option1: false,
                option2: 'left'
                // etc.
            },
            settings: {},
            init: function(options) {  
               /* see answer edit about moving this out of here*/
                methods.settings = $.extend({}, methods.defaults, options);
                doInit(this, 'init', methods);
    
            },
            refresh: function() {
                doInit(this, 'refresh', methods);
            },
            html: function(var1, var2) {
                // various HTML DOM manipulations
            },
            binders: function(var1, var2) {
                // binding various events, hover, click, etc.
                // assigns classes
            }
        };
    
        function doInit(el, initType, methods) {
            $(el).each(function() {
                if ($(this).is('[type=radio]')) {
                    var var1 = $(this);
                    var var2 = $('.somethingelse');
                    // etc.
                    methods.html(var1, var2);
                    if (initType == 'init') {
                        methods.binders(var1, var2);
                    }
                };
            });
        }
    
        $.fn.myPlugin = 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 jQuery.myPlugin');
            }
        };
    
    })(jQuery);
    

    May not be bug free but idea should get you going

    EDIT: Realized I forgot to make the settings more global. One way is to remove defaults from methods and create

      var defaults={  /* ..... */};
     $.fn.myPlugin.defaults=$.extend({},defaults, options);
    

    Now you have access to the user defined settings when you do the refresh. Another really helpful addition is to store the settings in jQuery DOM data like:

       $(this).data('myPlugin.settings', {/* extended settings object*/});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.