What happens when I apply https://api.jquery.com/data/ to a group of selected elements? Does it internally loop through the matched set and store an individual "copy" of the data on each element? Or does it someone store one copy applied to the matched set object? My little test case below seems to imply both is happening. Given that my desired outcome is to have the same data applied to each of the elements, is my first or second approach to set the data preferred from a speed/memory/semantic/etc standpoint?
http://jsfiddle.net/toyh8fty/1/
<p id="id1">1</p>
<p id="id2">2</p>
<p id="id3">3</p>
var v = {
a: 1,
b: 2,
c: 3
};
var p = $('p');
console.log(p);
p.data(v).each(function () {
var $t = $(this);
console.log(this, $t, $t.data());
});
console.log($('#id1').data());
console.log('p.data', p.data());
p.each(function () {
var $t = $(this);
$t.data(v);
console.log(this, $t, $t.data());
});
console.log($('#id1').data());
console.log('p.data', p.data());