Web Dev: Using data-* attributes – Web developer guide | MDN

JavaScript Access

Reading those out in JavaScript is also very simple. You could use getAttribute() to read them but the standard defines a simpler way: a DOMStringMap you can read out via a dataset property:

var article = document.querySelector('#electriccars'),
data = article.dataset;

// data.columns -> "3"
// data.indexnumber -> "12314"
// data.parent -> "cars"

Each property is a string (even if you omit the quotes in the HTML) and can be read and written. In the above case setting article.dataset.columns = 5 would change that attribute.

via Using data-* attributes – Web developer guide | MDN.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s