I have a global data holder element like that:
<div class="data-holder" data-id="" data-type="" data-age=""></div>
(It's longer but it's enough to get the idea)
Now I want to make a short function which will add values to those data attributes from other elements.
So for example if the user clicks an element with data-type="381"
it will add 381
to the global "data-holder"
element in the data-type
attribute. (each element has only one data attribute (data-type
, or data-age
and so...).
So I found this global function to remove all attributes from an element:
$.each($(this).data(), function (key) {
// Because each key is in camelCase,
// we need to convert it to kabob-case and store it in attr.
var attr = 'data-' + key.replace(/([A-Z])/g, '-$1').toLowerCase();
// Remove the attribute.
$(this).removeAttr(attr);
});
And I thought if I will use it like that:
$.each($(this).data(), function (key) {
// Because each key is in camelCase,
// we need to convert it to kabob-case and store it in attr.
var attr = 'data-' + key.replace(/([A-Z])/g, '-$1').toLowerCase();
// Remove the attribute.
// $(this).removeAttr(attr);
alert($(this).attr(attr));
});
I will get the current element's attribute value. but I get as "undefined".
Any ideas? Thanks!