attrobj
v3.1.0
Published
Creates an object based on element attributes
Downloads
2,199
Readme
attrobj
Transforms data-attributes into an array based on key.
Installation
npm install attrobj
Example usage
<div id="example" data-example-name="Test Name" data-example-color="red"></div>
const attrobj = require('attrobj');
const el = document.getElementById('example');
const exampleData = attrobj('example', el);
console.log(exampleData);
// { name: 'Test Name', color: 'red' }
Global Values
Data can be pulled from window.*
by using data-<somekey>-global-<valuename>
.
Example:
<div id="example2" data-weather-global-rain="rain" data-weather-cloudy="true"></div>
window.rain = '2.1';
const attrobj = require('attrobj');
const el = document.getElementById('example2');
const exampleData = attrobj('weather', el);
console.log(exampleData);
// { rain: '2,1', cloudy: 'true' }