Posts tagged with dom

Using the dataset property in JavaScript

In plain old JavaScript you can get the data attributes of

[code language="html"]

lorum ipsum
```

like this

[code language="javascript"] document.getElementById('test').getAttribute('data-news-item-id');



But did you know there's a alternative way of doing this by reading an element's dataset property?

[code language="javascript"]
document.getElementById('test').dataset.newsItemId;

Notice that the values can be accessed by camelcasing the name of the data attribute. You can also write to datasets to change values of data attributes. Read the informative helpful article on MDN to know more.

Read more