views:

216

answers:

3

My Javascript knowledge is pretty limited.

Instead of asking several javascript questions I got the "message" from Stack overflow and started using jQuery right away in order to save me some time.

However several times I do not undestand the "magic" behind jQuery and I would love to learn the details.

I want to use .data() in my application. The examples are very helpful. I do not understand however WHERE these values are stored.

I inspect the webpage with Firebug and as soon as .data() saves an object to a dom element, I do not see any change in Firebug (either HTML or Dom tabs).

I tried to look at jQuery source, but it is very advanced for my Javascript knowledge and I lost myself.

So the question is:

Where do the values stored by jQuery.data() actually go? Can I inspect/locate/list/debug them using a tool?

A: 

$(document).data() is one placed ive stored it

Lud
+5  A: 

Have a look at the source for it.

From a quick glimpse, it looks like it's storing the data in that cache variable that is created on line 2.

Edit:

Here's a quick demo that finds the data in the cache: http://jsfiddle.net/CnET9/

You can also dump $.cache to your console and explore it manually.

Matt
Nope, that cache variable is just some jQuery internal stuff. The data is in fact attached to a DOM element. BTW: Didn't he point out that the jQ source code wasn't helpful? So why point him there?
Techpriester
@Techpriester - You are incorrect, it **is** stored on the cache variable, using a key that represents your DOM element, it is **not** stored on the DOM element itself.
Nick Craver
Because I assume he's looking at the jQuery source directly, not the nicely organized source on GitHub.
Matt
Correction: I was wrong. Sorry ..
Techpriester
@Matt Yes you are right! I was looking a jQuery source directly instead of GitHub..
kazanaki
+1  A: 

You can inspect it by just calling .data() without params, like this:

$("div").data("thing", "value");​​​​​​
console.log($("div").data());
//or...
console.log($.data($("div").get(0)));

As for the "where", it's stored in a global jQuery cache object under a key that represents your element. Calling .data() really returns jQuery.data(yourDomElement), or tack on a key to that if you called for a specific value from it.

Nick Craver