25

Is it possible to loop through a data() object?

Suppose this is my code:

$('#mydiv').data('bar','lorem');  
$('#mydiv').data('foo','ipsum');  
$('#mydiv').data('cam','dolores');

How do I loop through this? Can each() be used for this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bart
  • 14,958
  • 21
  • 75
  • 105

6 Answers6

22
$.each($.data(this), function(i, e) {
   alert('name='+ i + ' value=' +e);
});

This will iterate through each property in 'this' element's data object.

Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79
John Strickler
  • 25,151
  • 4
  • 52
  • 68
  • 1
    This didn't work OOTB for me, I had to slightly modify it: `$.each($('#mydiv').data(), function (i, e) { alert(i + ":" + e); });` – saluce Mar 15 '13 at 18:11
15

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);
});

You could just use matt b's suggestion but this is how to do it with what you have right now.

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • If it's an internal variable, isn't that subject to change in later versions of jquery (probably without notice)? – matt b Apr 21 '09 at 14:16
  • I'm sure they could, but I'm not sure why they would. Any retooling they might do to the data functionality would likely include this functionality built-in anyways. I don't think it's really a valid reason to shy away from it, but I guess it's a concern to keep in mind. – Paolo Bergantino Apr 21 '09 at 14:21
10

Tested with jQuery 1.4 and tips from @user292614 the following works:

$('#mydiv').data('bar','lorem');  
$('#mydiv').data('foo','ipsum');  
$('#mydiv').data('cam','dolores');

$.each( $('#mydiv').data(),function(i, e) {
   alert('name='+ i + ' value=' +e);
});
Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79
9

I don't think that there is any function that gives you all of the "keys" of the data that has been added with the data() function, but instead, why not put all of your data into the function under an object / map?

something like this:

var container = new Object();
container.bar = "lorem";
container.foo = "ipsum";
container.cam = "dolores";
$("mydiv").data("container", container);

and then when you want to read the data / iterate through it:

var blah = $("mydiv").data("container");
for(key in blah) {
    var value = blah[key];
    //do whatever you want with the data, such as:
    console.log("The value of ", key, " is ", value);
}
matt b
  • 138,234
  • 66
  • 282
  • 345
0

I just tried this but needed some extra data values. If you also got this "problem" then the following should work.

$('#mydiv').data('bar', {name:'lorem', id:'156', price:'199'}); 

then you can simply extend with the value id

$.each( $('#mydiv').data(),function(i, e) {
   alert('name='+ i + ' name=' +e.name + ' id='e.id + ' price=' + e.price );
});
Marthin
  • 6,413
  • 15
  • 58
  • 95
0

If we use .data() means it store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.

and if .data() in loop so we have to access it same manner in loop so for e.g(below)

<p class="weekday" data-today="monday">Monday</p>
<p class="weekday" data-today="tuesday">Tuesday</p>
<p class="weekday" data-today="wednesday">Wednesday</p>
<p class="weekday" data-today="thursday">Thursday</p>

The html in my loop and data-today is same in all tags but their values are different so basically it is loop generated html so we have to access in same manner i.e loop in js/jQuery for e.g. (below jQuery code)

$('.weekday').each(function(){ $(this).data('today'); });

OutPut :

Monday
Tuesday
Wednesday
Thursday

NOTE : In browser console it return particular <DIV>.

Vrushal Raut
  • 1,050
  • 2
  • 15
  • 20