1

http://api.jquery.com/data/

I'm trying to do something like this with the data method:

$("body").data("bar", { myType: "test", count: 40 });

However, I can't seem to set "count" like this:

$("body").data(bar["count"], 41); 

Is my syntax wrong?

1
  • You cant do $("body").data(bar["count"], 41); first argument need to be a string. Commented Apr 30, 2013 at 21:40

2 Answers 2

5

The syntax is .data( key, value ). data(bar["count"], 41);is equal to data(40, 41);.

If you want to change the value of your object you can do $("body").data("bar").count = 41

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

Comments

0

Looks like you need to use it like this:

$("body").data({ baz: [ 1, 2, 3 ] });

// then

var data = $("body").data();
console.log(data.baz);  // outputs the array [ 1, 2, 3 ]

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.