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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:10:28+00:00 2026-05-13T17:10:28+00:00

I have created a jQuery plugin that works great with the exception of being

  • 0

I have created a jQuery plugin that works great with the exception of being able to call the plugin on different objects and each object retaining the options it was given. The problem is that if I call the plugin on one object, say:

$('#myDiv1').myPlugin({
    option1: 'some text',
    option2: true,
    option3: 'another option value'
});

then call the plugin again on another object, say:

$('#myDiv2').myPlugin({
    option1: 'different text',
    option2: false,
    option3: 'value for myDiv2'
});

Then if I go back and try to do something with #myDiv1 that needs its original options to still be intact, ie:

$('#myDiv1').myPlugin.update();

it won’t have it’s original options, but they will be overridden by the options for #myDiv2. What’s the proper way to do this so that each object will retain the original options given to it? (And here’s some example code of what I’m doing in the plugin)

(function($) {
$.fn.myPlugin = function(options) {

    // build main options before element iteration
    var opts = $.extend({}, $.fn.myPlugin.defaults, options);

    _option1 = opts.option1;
    _option2 = opts.option2;
    _option3 = opts.option3;

    // iterate all matched elements
    return this.each(function() {
        callPluginFunctions( this, opts );
    });

};

    ....code continued....

I realize this is some kind of scope creep or something. So, how do I get my options to stay attached and remain in the scope of the original object (ie #myDiv1) that they were given to.

EDIT: In doing some research I see that you can store data to an object using jQuery’s .data function, and the docs say jQuery UI uses it extensively. Would the proper thing to do here be store the options on the object using .data, then when referenced later use the options stored in .data ???

  • 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-05-13T17:10:28+00:00Added an answer on May 13, 2026 at 5:10 pm

    First, you will generally want to handle the command within your extension method. Second, you should be attaching configurations to each item…

    (function($){
    var defaultOptions = { /* default settings here */ };
    
    //called on the native object directly, wrap with $(obj) if needed.
    function initializeObject(obj, options) {
      //assign the options to the object instance.
      $(obj).data('myPlugin-options', $.extend(defaultOptions, options) );
      //do other initialization tasks on the individual item here...
    }
    
    function updateObject(obj) {
      // use $(obj).data('myPlugin-options');
    }
    
    function setOption(obj, key, value) {
      var d = $(obj).data('myPlugin-options');
      d[key] = value;
      $(obj).data('myPlugin-options', d);
    }
    
    $.fn.myPlugin = function(command, option, val) {
      if (typeof command == "object") {
        //initialization
        return this.each(function(){
          initializeObject(this, command);
        });
      }
      if (typeof command == "string") {
        // method or argument query
        switch (command.toLowerCase()) {
          case 'option':
            //get value, return the first item's value
            if (typeof val == undefined) return this.eq(0).data('myPlugin-options')[option];
            //set option value
            return this.each(function() {
              setOption(this, option, val);
            });
    
          case 'update':
            return this.each(function() {
              updateObject(this);
            });
          //other commands here.
        }
      }
    }
    })(jQuery)
    

    With the above example, you have a generic template for a jQuery extension, It’s usually good form to have the following convention for use..

    Initialization:
      $(...).myPlugin({ initialization-options-here });
    Command:
       $(...).myPlugin('command-name'); //where command can be update, etc.
    Get Option:
      var myValue = $(...).myPlugin('option', 'option-name');
    Set Option:
      $(...).myPlugin('option', 'option-name', newValue);
    

    Updated to use .data off of each individual obj.

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

Sidebar

Related Questions

I have this jQuery ajax navigation tabs plugin that I created using some help
I have created a paging grid in asp.net with this jquery plugin(https://github.com/gbirke/jquery_pagination),that work fine.But
I have created a custom jQuery plugin that does some simple image resizing for
I have created a jQuery plugin that grabs a JSON feed (in this case
I have created a jQuery plugin that is used to create html on a
I have created a jquery plugin to expand/collapse ul,li something like a accordion menu
I am using the jQuery Tools Scrollable plugin - http://flowplayer.org/tools/scrollable.html#navigator I have created a
I have created some JQuery that will expand a div 'popup' on hover and
I have created a jQuery animation that can be seen by clicking the Preview
I have a form with a Jquery plugin requirement that creates a form input

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.