Search code examples
jqueryjquery-data

jQuery .data() assignments work in console print but produces errors


What is wrong with these jQuery data() assignments? When I wrap the first in a console.log(), it works and prints out the data.

But jsbin shows tons of errors that I can't put my finger on?

$("body").data("user_information", {});
$("body").data("user_information", {
    contact_info: {},
    billing_info: {}
});

console.log($("body").data("user_information").contact_info, {name: "paul", company: "testCo"});

$("body").data("user_information").contact_info, {
    name: "paul",
    company: "testCo"
};

$("body").data("user_information").billing_info, {
    name: "steve",
    company: "testCo"
};

jsbin


Solution

  • If you're trying to do assignment, you should do it like this:

    $("body").data("user_information").contact_info = {
        name: "paul",
        company: "testCo"
    };
    

    The comma operator you were using there was confusing me. Alternatively, you could do

    $("body").data("user_information", {
            contact_info: {
                name: "paul",
                company: "testCo"
            }
    });