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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:21:08+00:00 2026-06-17T08:21:08+00:00

I want to create two JQuery functions. One of which will use the data

  • 0

I want to create two JQuery functions. One of which will use the data created in the other function. Say:

jQuery.fn.myScroller = function (child) {
    var $moveable = this.children(child);
    var $children = this.children(child).children();

    posl = posl.replace('px', '');
    post = post.replace('px', '');
    var varList = [];
    $children.each(function () {
        var currVar = {};
        currVar.w = $(this).width();
        varList.push(currVar);
    });
    // I want to save varList variable somehow.
    this.varList = varList; // (1)
};

jQuery.fn.scrollUnitLeft = function () {
    // Now I need to use this varList if this function called
    // But the following returns undefined.
    // However, I've saved it inside this at (1)
    console.log(this.varList)
// More code...
}


$('#main_div').myScroller("#dummydiv");
$('#main_div').scrollUnitLeft();

As I explained in comments in the code, this doesn’t work.

How can I do this?

  • 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-17T08:21:09+00:00Added an answer on June 17, 2026 at 8:21 am

    When you run your jQuery functions, this refers to the jQuery object instance used in each case.

    In your code you create two jQuery object instances (one for every $(...) call), so the data is set in the first instance, and hence not available to the second instance.

    If you were to run both your methods on the same jQuery object instance, they would work as expected.

    jQuery.fn.mySet = function(){
      this.myVar = this.attr('id');
    };
    
    jQuery.fn.myGet = function(){
      console.log(this.myVar);
    }
    
    $('#a_div').mySet();
    $('#a_div').myGet(); // outputs 'undefined'
    
    var $other_div = $('#other_div');
    $other_div.mySet();
    $other_div.myGet(); // outputs 'other_div'
    

    To achieve what you intend, you have to persist the data someplace other than the jQuery object instance. jQuery offers a way to do this via the .data() method. This method allows you to attach data to DOM elements. Check its documentation.

    jQuery.fn.myRealSet = function(){
      this.data('myVar', this.attr('id'));
    };
    
    jQuery.fn.myRealGet = function(){
      console.log(this.data('myVar'));
    }
    
    $('#final_div').myRealSet();
    $('#final_div').myRealGet(); // outputs 'final_div'
    

    You can test this snippets here: http://jsfiddle.net/HPjYn/

    EDIT: I cannot yet comment on other people’s answers, but adding a jQuery prototype varable as suggested will make the data available from any jQuery object instance, which I don’t think is what it’s intended here.

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

Sidebar

Related Questions

I have two functions, one creates paragraphs dynamically and the other function selects (changes
I want to create two widgets in flex. One for the movies ( that
i am preparing my android apps in which i want to create two row
I want to create a two-dimensional array in which I want to store records
Theoretically speaking let's say I have 3 javascript (jquery) functions that create visual effects.
I want to create two new methods for jQuery that makes appear and disappear
I have two JQuery autocomplete's on a screen. One is mandatory, the other is
I want to create jquery autocomplete and php, the problem is the data shown
I want to create two equal timestamps for a given day in PHP and
I want to create two and three dimensional vectors using a constructor in a

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.