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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:27:26+00:00 2026-05-24T10:27:26+00:00

The jQuery documentation suggests storing additional info per DOMElement using data() . But I’m

  • 0

The jQuery documentation suggests storing additional info per DOMElement using data(). But I’m having a hard time accessing the saved data in a good way.

The scope changes when I call other functions which makes me lose my way 🙂

(function ($) {
    var methods = {
        init: function (options) {
            return this.each(function () {
                var $this = $(this),
                    data = $this.data('myPlugin');

                if (!data) {
                    $(this).data('myPlugin', {
                        testValue: true
                    });

                    data = $this.data('myPlugin');
                }
            });
        },

        testFunction: function () {
            console.log('t: '+$(this).data('myPlugin').testValue);
            methods.otherFunction();
        },

        otherFunction: function () {
            console.log('o: '+$(this).data('myPlugin').testValue);
        }
    };

    $.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);

$(document).ready(function () {
    $('body').myPlugin();
    $('body').myPlugin('testFunction');
});

Console output:

t: true
Uncaught TypeError: Cannot read property 'testValue' of undefined
  • 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-24T10:27:26+00:00Added an answer on May 24, 2026 at 10:27 am

    You need to use

            methods.otherFunction.apply(this);
    

    instead of

            methods.otherFunction();
    

    to make the scopes correct.

    Demo: http://jsfiddle.net/ayNUD/

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

Sidebar

Related Questions

I'm using the BeautyTips jQuery extension, but I'm not having any luck passing dynamic
I have always used the mouseover event, but while reading the jQuery documentation I
I have tried to find some decent documentation on traversing in jQuery, but have
I've read the jQuery documentation on event handling, but I still can't really understand
When using the example code from the jQuery documentation for the keypress event handler,
The jQuery documentation lists the following example of using $.getJSON to request JSONP: $.getJSON(http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?,
jQuery documentation indicates the following is returned: success(data, textStatus, XMLHttpRequest)Function A function to be
I'm using the following jQuery code to try and access the delicious api, but
I'm using the Jquery UI Accordion, and I haven't found anywhere in the documentation
The jQuery documentation covers the function jQuery.extend()s twice, giving it different definitions. The first

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.