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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:23:18+00:00 2026-06-09T23:23:18+00:00

I am wondering how to share data between methods in a jQuery extension, so

  • 0

I am wondering how to share data between methods in a jQuery extension, so when I call the refresh method, it is able to use the plugin and options variables defined in init.

There is more than one of these controls per page.

Here is an example of what I have:

(function($) {
    var methods = {
        init: function(options) {
            var defaults = {
                defaultText: "Rating",
                maxRating: 5,
            };

            options = $.extend(defaults, options);

            return this.each(function() {
                var plugin = $(this);

                main($(plugin), options);
            });
        },

        refresh: function() {
            // I would like to be able to use the 'options' and 'plugin' variables defined in the init method
        }
    };

    function main(plugin, option) {
        // do main code stuff
    }
}(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-09T23:23:19+00:00Added an answer on June 9, 2026 at 11:23 pm

    Store the data on the element itself, then access it per element.

    (function ($) {
    
    var methods = { // should probably expose this object in some way so it can be extended by other develoeprs or so that defaults can be changed
    
        defaults : {
            defaultText: "Rating",
            maxRating: 5,
        },
    
        init : function (options) {
    
            options = $.extend({}, methods.defaults, options);
    
            this.data("myPlugin", options);
    
            return this.each(function () {
    
                main($(this), options);
    
            });
        },
    
        refresh : function () {
    
            return this.each(function(){
    
                var options = $(this).data("myPlugin");
    
            });
                    // I would like to be able to use the 'options' and 'plugin' variables defined in the init method
        }
    };
    
    function main(plugin, option) {
    
        // do main code stuff
    
    }
    
    })(jQuery);
    

    I’m not sure why you would want access to the plugin var, you should already have access to the element that the refresh method is called on.

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

Sidebar

Related Questions

So I'm using static class members so I can share data between class methods
I'm looking for suggestions for ways to share Android app data between phones running
I searched everywhere and wondering whether any posible way available for share data among
I was wondering if people would share their best practices / strategies on handling
I just started implementing a share functionality but was wondering if its possible to
Hi brothers and sisters, I am wondering if anyone would share their experience on
Wondering what the best / good way of doing this would be in jQuery.
Wondering if someone could please explain the difference between these two queries and advise
I read: iPhone: How to Pass Data Between Several Viewcontrollers in a Tabbar App
I'm designing data-oriented application in Silverlight 3 and I'm wondering if is it worth

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.