get-data-attributes
v1.0.1
Published
Minimal function that returns an element's data attributes as an object
Downloads
9
Readme
get-data-attributes
Minimal function that returns an element's data attributes as an object.
Make sure you escape your attributes if they're not extremely simple.
https://henrygd.me/get-data-attributes
Install
$ npm install get-data-attributes
Usage
import getDataAttributes from 'get-data-attributes'
<div
id="example"
data-example="one"
data-my-string="it is a string"
data-my-array='["one", true, 3]'
data-my-object='{"The Last of Us": 10, "The Last of Us Part II": 10, "10 of 10 games": ["The Last of Us", "The Last of Us Part II"]}'
></div>
let data = getDataAttributes(document.getElementById('example'))
// data will be this
{
example: "one",
myString: "it is a string",
myArray: [
"one",
true,
3
],
myObject: {
"The Last of Us": 10,
"The Last of Us Part II": 10,
"10 of 10 games": [
"The Last of Us",
"The Last of Us Part II"
]
}
}
License
MIT