set data attribute js code example
Example 1: how to set data attribute in js using setattribute
// setting data-foovar el = document.querySelector('div');el.setAttribute('data-foo', 'Hello World!');
Example 2: javascript change data attribute value
element.dataset.keyName = 'newValue';
Example 3: accèder data-id javascript
<article
id="voitureelectrique"
data-columns="3"
data-index-number="12314"
data-parent="voitures">
...
</article>
var article = document.getElementById('voitureelectrique');
article.dataset.columns // "3"
article.dataset.indexNumber // "12314"
article.dataset.parent // "voitures"