get-data-attributes

1.0.1 • Public • Published

get-data-attributes npm File Size

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

Readme

Keywords

none

Package Sidebar

Install

npm i get-data-attributes

Weekly Downloads

4

Version

1.0.1

License

MIT

Unpacked Size

211 kB

Total Files

13

Last publish

Collaborators

  • henrygd