I just can't wrap my head around this. I tried to add a data-diff attribute to some elements. Seems to work, yet no such attribute appears in the html. No big deal I thought, opened developer tools and my trail of thought went like this:
Lets set this attribute again:
console: $(obj).data('diff','10');
output: [div.cont]
Good, let's check the attribute then:
console: $(obj).data('diff')
output: "10"
Great, but still it doesn't appear in HTML, let's check this:
console: $(obj)[0]
output:
<div class="cont" data-month="8" data-round="1">
(Tom) 8
<div class="secCol">AUG</div>
</div>
Hmm, indeed there is no data-diff, maybe if I try this:
console: $(obj)[0].data('diff','10')
output: Uncaught TypeError: $(...)[0].data is not a function(…)
I guess it has something to do with DOM elements vs jQuery objects (already read this) but I don't know what else to try. My code is this:
$('.cont[data-round="'+round+'"]').each(function(i, obj) {
var month = $(obj).data('month');
var diff = Math.abs(myMonth-month);
$(obj).data('diff', diff);//Here is the problem
});
$(obj)[0]gives you the HTML element,.data()is a jQuery function. Workaround:$($(obj)[0]).data('diff')