Skip to content

Instantly share code, notes, and snippets.

@goldhand
Created March 18, 2016 16:18
Show Gist options
  • Save goldhand/79d684b6863223ae40e3 to your computer and use it in GitHub Desktop.
Save goldhand/79d684b6863223ae40e3 to your computer and use it in GitHub Desktop.
collect dataset attributes from a dom element
const getDataset = (elem) => {
/* ie <= 10 does not support elem.dataset
* ie <= 10 also doesn't support Array.from
* collect an elements data properties and return them in a similar format
* to the elem.dataset method
*/
let attrs = Array.prototype.slice.call(elem.attributes).filter(attr => !!~attr.nodeName.indexOf('data-'));
return attrs.reduce((a, attr) => {
let props = {};
props[attr.nodeName.substring(5)] = attr.value;
return {
...a,
...props,
}
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment