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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:17:24+00:00 2026-05-25T00:17:24+00:00

I am working on a jQuery plugin, but I am having some trouble with

  • 0

I am working on a jQuery plugin, but I am having some trouble with the design pattern, which combines the defaults and options, data and namespacing techniques from jQuery’s best practices. Here’s the abstracted version of my code:

(function($) {
var defaults = {
    key1: 'value1',
    key2: 'value2'
};
var settings = {};
var methods = {
    init: function() {
        return this.each(function() {
            var $this = $(this), data = $this.data('pluginname');
            console.log('init called');
            // If the plugin hasn't been initialized yet
            if (!data) {
                //Do more setup stuff here
                $this.data('pluginname', {
                    key3: 'value3',
                    key4: 'value4'
                });
            }
            //test for settings and data
            console.log(settings);
            console.log(data);
        });
    },
    methodname: function() {
        return this.each(function() {
            var $this = $(this), data = $this.data('pluginname');
            console.log('methodname called');
            //test for settings and data
            console.log(settings);
            console.log(data);
        });
    }
};
$.fn.pluginname = function(method, options) {
    settings = $.extend({}, defaults, options);
    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.pluginname' );
    }    
};
$(document).ready(function() {
    $('body').pluginname();
    $('body').pluginname('methodname', {key1: 'overriding default value for key 1'});
});
})(jQuery);

If you run that code with the latest version of jQuery you will see that I am calling both the init and methodname functions and logging the settings and data objects in each. In the methodname call, I can access both settings and data, but in the init call itself, the data object returns undefined. If I set a breakpoint in the script at line 21, I can call back the data object with $this.data('pluginname') in the console. Anyone see what I am doing wrong here? I should just be able to write data.key3 inside the init, function, right?

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

    In your init method, at the very beginning of the .each loop you assign the object stored in the currently iterated element to your data variable. If there is no data available for the specified key, data is undefined.

    You then test data for truthiness, and if it is evaluates to false you go ahead and assign a data object to the current element.

    Then later on, you call console.log(data); and it gives you undefined. This is expected, as data was initially assigned undefined – and that’s what it still refers to. To fix:

    // if undefined
    if (!data) {
        //Do more setup stuff here
    
    
        $this.data('pluginname', {
            key3: 'value3',
            key4: 'value4'
        });
        data = $this.data('pluginname');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a jQuery plugin, but am having some trouble getting my variables
I'm using the jQuery Countdown plugin but having some trouble getting it to show
I'm creating a jQuery plugin. So far it's working fine, but I'm having doubt
I'm having a little trouble with a jQuery plugin wrote. I've got it working
I am using a plugin called jQuery contextMenu but am having trouble making it
i m using jQuery tablsorter plugin, it's working perfect,but now problem is ... i
I don't know what is going but my jquery validate plugin(1.5.5) is not working
I'm working with the jQuery Validation plugin and I wrote the following code which
I seem to be having some problems creating a jquery plugin. I am testing
I'm having trouble getting jquery.validation.js to validate a select box. It is working on

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.