2

I'm using jQuery's .data() method to store some data using nested objects like this:

$('div.divwithdata').data('somedata', {
    'a': {
        'b': {
            'c' : {}
        }
    }
});

However my question is: Is there a way to replace nested object's property without overwriting the whole object ?

I tought it could be done with .data('somedata.a.b', 'newdata') or .data('somedata[a][b]', 'newdata'), but none of them worked.

jsFiddle example

2 Answers 2

3

$('div.divwithdata').data('somedata').a = something... or $('div.divwithdata').data('somedata').a.b.c = "test";

This works;

Sign up to request clarification or add additional context in comments.

Comments

2

Since data() stores references, you can use its getter form and directly update the object it returns:

$("div.divwithdata").data("somedata").a.b = "newdata";

Or, using bracket notation:

$("div.divwithdata").data("somedata")["a"]["b"] = "newdata";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.