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

Olivvv
  • 1,140
  • 1
  • 13
  • 37
  • 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. – John Saunders Jun 16 '09 at 04:19
  • He is referring to this: http://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 – matt b Jun 16 '09 at 04: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. – Olivvv Jun 16 '09 at 04:32

1 Answers1

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'});
Community
  • 1
  • 1
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • 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 :) – matt b Jun 16 '09 at 04:34
  • @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! – Paolo Bergantino Jun 16 '09 at 04:50
  • 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 Jun 17 '09 at 00:28
  • @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. – Paolo Bergantino Jun 17 '09 at 00:48
  • Do you know why and when jQuery is adding this data ? – Olivvv Jun 17 '09 at 01:55
  • No, I'm afraid I don't. Sorry. – Paolo Bergantino Jun 17 '09 at 02:06