0

all is said in the title, I want to get from an element all the data set using the data method.

(ultimately I want to copy that data over to a newly created element)

thanks for any help !

Olivier

3
  • 3
    Please clarify your question. I'm very sure that I don't know what you mean; I'm not sure about anything else, though. Commented Jun 16, 2009 at 4:19
  • He is referring to this: docs.jquery.com/Core/data#name I'm pretty confident that a very similar question to this has been asked before, which would solve your problem - but I'm having trouble finding it Commented Jun 16, 2009 at 4:22
  • John I'm trying to be as explicit as possible. There is in jQuery a method named data, which is used to store data into elements. For a given element, I want to retrieve all the data stored, not only the data corresponding to a specific key. Commented Jun 16, 2009 at 4:32

1 Answer 1

4

This has been asked before. My answer from there, since it is a good question:

jQuery stores all the data information in the jQuery.cache internal variable. It is possible to get all the data associated with a particular object with this simple but helpful plugin:

jQuery.fn.allData = function() {
    var intID = jQuery.data(this.get(0));
    return(jQuery.cache[intID]);
};

With this in place, you can do this:

$('#myelement').data('test1','yay1')
               .data('test2','yay2')
               .data('test3','yay3');

$.each($('#myelement').allData(), function(key, value) {
    alert(key + "=" + value);
});

Alternatively, you can simply store an object:

$('#myelement').data('data', {test1:'yay1',test2:'yay2',test3:'yay3'});
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for finding what I was looking for! How ironic that the two of us were the ones trying to find it, since we answered the original :)
@Oli: No problem. @matt b: Yeah, google search works wonders over stackoverflow search, just do site:stackoverflow.com jquery all data and the old question comes up first. It's like magic!
I'm not sure but I suspect the plugin to retrieve "too much" data, data that I haven't set but data that is used internally by jQuery. It is still a bit confused for me but I think that somehow jQuery is storing expando's with one element's data. So allData retrieves it too, giving me value/keys such as : jQuery12345197113605 / 572. that thing "jQuery12345197113605" is a uniqueid, so it seems difficult to filter out efficiently. That's all what I have for now.
@Olivvv: I don't think there's any way to differentiate between data that you have set and data that jQuery has set. If this is a problem, you should just go with the second suggestion of storing elements in an object. If that is unacceptable, you can try filtering out any variables that start with jQuery.
Do you know why and when jQuery is adding this data ?
|

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.