data-attributes

1.0.0 • Public • Published

data-attributes Build Status

Extract data attributes from a DOM node.

Follows the data-* spec.

Install

Via npm:

$ npm install data-attributes --save

Via Bower:

$ bower install data-attributes --save

Usage

<button data-loading-label="Loading..." data-color="#F06">Click me</button>
 
<script>
  var button = document.querySelector('button');
  var data = dataAttributes(button);
 
  data(button); // {loadingLabel: "Loading...", color: "#F06"}
</script> 

API

dataAttributes(element)

Returns an object containing all data attributes (with camel case keys) from a given DOM node.

Motivation

I needed something like jQuery's $.data() to simply retrieve data attributes from DOM elements. It was for a very old project that needs to support IE7.

If you do not care about IE7, you can simply use dataset.

License

MIT © Rafael Rinaldi

Package Sidebar

Install

npm i data-attributes

Weekly Downloads

46

Version

1.0.0

License

MIT

Last publish

Collaborators

  • rafaelrinaldi