Given an arbitrary HTML element with zero or more data-*
attributes, how can one retrieve a list of key-value pairs for the data.
E.g. given this:
<div id='prod' data-id='10' data-cat='toy' data-cid='42'>blah</div>
I would like to be able to programmatically retrieve this:
{ "id":10, "cat":"toy", "cid":42 }
Using jQuery (v1.4.3), accessing the individual bits of data using $.data()
is simple if the keys are known in advance, but it is not obvious how one can do so with arbitrary sets of data.
I'm looking for a 'simple' jQuery solution if one exists, but would not mind a lower level approach otherwise. I had a go at trying to to parse $('#prod').attributes
but my lack of javascript-fu is letting me down.
update
customdata does what I need. However, including a jQuery plugin just for a fraction of its functionality seemed like an overkill.
Eyeballing the source helped me fix my own code (and improved my javascript-fu).
Here's the solution I came up with:
function getDataAttributes(node) {
var d = {},
re_dataAttr = /^data\-(.+)$/;
$.each(node.get(0).attributes, function(index, attr) {
if (re_dataAttr.test(attr.nodeName)) {
var key = attr.nodeName.match(re_dataAttr)[1];
d[key] = attr.nodeValue;
}
});
return d;
}
update 2
As demonstrated in the accepted answer, the solution is trivial with jQuery (>=1.4.4). $('#prod').data()
would return the required data dict.
You should be get the data through the dataset attributes
dataset is useful tool for get data-attribute
Have a look here:
If the browser also supports the HTML5 JavaScript API, you should be able to get the data with:
or
Oh, but I also read:
It is from May 2010.
If you use jQuery anyway, you might want to have a look at the customdata plugin. I have no experience with it though.
You can just iterate over the data attributes like any other object to get keys and values, here's how to do it with
$.each
:One way of finding all data attributes is using
element.attributes
. Using.attributes
, you can loop through all of the element attributes, filtering out the items which include the string "data-".A pure JavaScript solution ought to be offered as well, as the solution is not difficult:
This gives an array of attribute objects, which have
name
andvalue
properties:Edit: To take it a step further, you can get a dictionary by iterating the attributes and populating a data object:
You could then access the value of, for example,
data-my-value="2"
asdata.myValue
;jsfiddle.net/3KFYf/33
Edit: If you wanted to set data attributes on your element programmatically from an object, you could:
jsfiddle.net/3KFYf/34
EDIT: If you are using babel or TypeScript, or coding only for es6 browsers, this is a nice place to use es6 arrow functions, and shorten the code a bit:
As mentioned above modern browsers have the The HTMLElement.dataset API.
That API gives you a DOMStringMap, and you can retrieve the list of
data-*
attributes simply doing:you can also retrieve a array with the
data-
property's key names likeor map its values by
and like this you can iterate those and use them without the need of filtering between all attributes of the element like we needed to do before.