Created
March 18, 2016 16:18
-
-
Save goldhand/79d684b6863223ae40e3 to your computer and use it in GitHub Desktop.
collect dataset attributes from a dom element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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