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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:25:29+00:00 2026-06-07T00:25:29+00:00

Original question: Note: Below plugin pattern based on the official jQuery docs . I’m

  • 0

Original question:

Note: Below plugin pattern based on the official jQuery docs.

I’m stuck… How can I convert the below plugin pattern to allow $.hooplah.defaults.whoop = 'there it was';?

;(function($) {

    var config = {};

    config.defaults = {
        foo   : 'bar',
        hey   : 'ho',
        whoop : 'there it is'
    };

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

    };

    var methods = {

        init: function(opts) {

            return this.each(function() {

                var options = $.extend({}, config.defaults, opts);

                // Stuff here...

            });

        }

    };

})(jQuery);

Optimally, I would like to do both $('.foo').hooplah({ whoop: 'there is was' }) and $.hooplah.defaults.whoop = 'there it was';.

Any tips/code/links would be greatly appreciated. 🙂

Many thanks in advance for the help!


Proposed solution #1

Calling syntax: $.pluginName.defaults

Code based on @AmithGeorge’s reply:

;(function($) {

    var config = {};

    config.others = {
        blah : 'nah',
        cha  : 'right!',
        last : 'one'
    }

    config.defaults = {
        foo   : 'bar',
        hey   : 'ho',
        whoop : 'there it is'
    };

    $.hooplah = {};
    $.hooplah.defaults = config.defaults;
    $.hooplah.others   = config.others;

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

    };

    var methods = {

        init: function(opts) {

            return this.each(function() {

                var defaults = $.extend({}, config.defaults, config.others, $.hooplah.defaults, $.hooplah.others);

                // After some lines...

                var options = $.extend({}, defaults, opts);

                console.log(options);

                // Stuff here...

            });

        }

    };

})(jQuery);

HTML:

<ul id="nav><li>...</li></ul>

<script type="text/javascript">
    <!--

        $(document).ready(function() {

            $.hooplah.defaults.foo = 'foooooo';
            $.hooplah.defaults     = { whoop : 'what?' };
            $.hooplah.others.blah  = 'why?';
            $.hooplah.others       = { cha : 'ok' }
            $nav = $('#nav');
            $nav.hooplah({
                hey  : 'hey hey',
                last : 'first'
            });

        });

    //-->
</script>

Output before:

blah     "nah"
cha      "right!"
foo      "bar"
hey      "ho"
last     "one"
whoop    "there it is"

Output after:

blah     "why?"
cha      "ok"
foo      "foooooo"
hey      "hey hey"
last     "first"
whoop    "what?"

Proposed solution #2

Calling syntax: $.fn.pluginName.defaults

Code based on @JonJaques reply:

;(function($) {

    var config = {};

    config.others = {
        blah : 'nah',
        cha  : 'right!',
        last : 'one'
    }

    config.defaults = {
        foo   : 'bar',
        hey   : 'ho',
        whoop : 'there it is'
    };

    var methods = {

        init: function(opts) {

            return this.each(function() {

                var options = $.extend({}, config.defaults, config.others, $.fn.hooplah.defaults, $.fn.hooplah.others, opts);

                console.log(options);

                console.log(config.others.last); // Outputs "one".

                // Stuff here...

            });

        }

    };

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

    };

    $.fn.hooplah.defaults = config.defaults;

    $.fn.hooplah.others = config.others;

})(jQuery);

HTML:

<ul id="nav><li>...</li></ul>

<script type="text/javascript">
    <!--

        $(document).ready(function() {

            $nav = $('#nav');

            $.fn.hooplah.defaults.foo = 'foooooo';
            $.fn.hooplah.defaults     = { whoop : 'what?' };
            $.fn.hooplah.others.blah  = 'why?';
            $.fn.hooplah.others       = { cha : 'ok' }
            $nav.hooplah({
                hey  : 'hey hey',
                last : 'first'
            });

        });

    //-->
</script>

Output before:

blah     "nah"
cha      "right!"
foo      "bar"
hey      "ho"
last     "one"
whoop    "there it is"

Output after:

blah     "why?"
cha      "ok"
foo      "foooooo"
hey      "hey hey"
last     "first"
whoop    "what?"

Awesome. 🙂

Let me know if the above code samples could be improved and/or if I overlooked something.

Thank you @AmithGeorge and @JonJaques! You folks seriously ROCK!

  • 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-07T00:25:30+00:00Added an answer on June 7, 2026 at 12:25 am

    Why cant you just do this?

    config.defaults = {
        foo   : 'bar',
        hey   : 'ho',
        whoop : 'there it is'
    };
    
    $.hooplah.config.defaults = config.defaults;
    

    And in your init function, the first line will be

    var defaults = $.extend({}, config.defaults, $.hooplah.config.defaults);
    // after some lines
    var options = $.extend({}, defaults, opts);
    

    The advantage I see of doing it this way is, if some one were to replace the $.hooplah.config.defaults with an object that doesnt have the same keys as your config, your plugin can still get the default config values from the private variable.

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

Sidebar

Related Questions

Note: Full working example now below. Original question follows: I'm having problems using ld's
My original question can be found here , for which I've gotten some great
Edit: original question below, but I revise it now that I have some code
[Note: This question had the original title " C (ish) style union in C#
EDIT 3: I am maintaining the original question below for historical reasons. However, I
UPDATED - please read further details below original question I have a select form
[EDITED: I left the original question below, with some more context and code to
Update is below code chunks. Original Question: I have an input field that is
EDIT: See my answer below for the hotfix. ORIGINAL QUESTION: In setting up for
NOTE: the original question is moot but scan to the bottom for something relevant.

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.