JsCoder
JsCoder

Reputation: 1733

JQuery data() call

Could anyone explain the difference between this:

Update 1- reverting phrases

container.data("state", { test: 1 }); // works

and this:

 container.data["state"] = { test: 1 }; // doesn't work

I found that the first construction works, while the last one doesn't. The call populates container with the initial data, i.e. "state" is blank when it's called.

Upvotes: 2

Views: 47

Answers (1)

Chris Laplante
Chris Laplante

Reputation: 29658

data is a function, not an array. You're trying to access an index of a function in the second snippet, which doesn't work.

See the docs: http://api.jquery.com/jQuery.data/

For example, open up your Development Console in your browser, and type something like this:

$("a:eq(0)").data

I get a long function definition, not an array:

function (a,c){var d,e,g,h=null;if(typeof a=="undefined"){if(this.length){h=f.data(this[0]);if(this[0].nodeType===1&&!f._data(this[0],"parsedAt.... // continued

Upvotes: 2

Related Questions