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

  • Home
  • SEARCH
  • 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 6692721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:54:01+00:00 2026-05-26T05:54:01+00:00

I have a jQuery object $myObject that contains the elements el_1, el_2, …, el_n

  • 0

I have a jQuery object $myObject that contains the elements el_1, el_2, ..., el_n. Doing

$myObject.data('foo', 'bar');

will assign the value 'bar' to the data property foo for each element el_1, el_2, ..., el_n. Instead, I would like to associate data to the jQuery object $myObject as a single entity.

I guess I could do

$myObject['foo'] = 'bar';

but this may cause collisions with other properties and methods of the jQuery object. Is there a “jQuery-safe way” to associate data with a jQuery object?

  • 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-26T05:54:01+00:00Added an answer on May 26, 2026 at 5:54 am

    You can store that data in your own object, using the jQuery object references as keys, then store that object in the document’s data. Something like:

    $.fn.objData = function(key, value) {
        var rootData = $(document).data("jQueryObjectData");
        if (!rootData) {
            $(document).data("jQueryObjectData", rootData = {});
        }
        var objData = rootData[this];
        if (!objData) {
            rootData[this] = objData = {};
        }
        if (typeof value === "undefined") {
            return objData[key];
        }
        objData[key] = value;
        return this;
    };
    

    Then you can use the method as expected:

    $myObject.objData("foo", "bar");
    

    And later:

    var foo = $myObject.objData("foo");
    

    Of course, $myObject must refer to the very same object in the two calls, as two distinct jQuery objects containing the same elements will still count as different keys.

    A potential problem with this approach is that the root data object will keep a reference to the jQuery objects used as keys, thus preventing the garbage collector from disposing them. It might prove to be a issue depending on the number of jQuery objects you create and the way you use the method.

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

Sidebar

Related Questions

I have a selection of elements in a jQuery object. I want to animate
I have jQuery to select all font elements that are children of the element
I have an object that I want to send with my jquery.ajax function but
I have an object that looks like this: var MyObject = { prop1 =
If I have code that pulls down a jQuery object, and then makes some
I have a jQuery object (named wid ) that has a code similar to
I have the following object: Test.Beta.MyObject = function($) { var foo = {}; foo.blah
Say I have a jQuery object of uncertain contents (from something that could be
I'm struggling to find the right terminology here, but if you have jQuery object...
I have a method, which will accept a parameter of a JQuery Object and

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.