1var element = document.querySelector('.element');
2var dataAttribute = element.getAttribute('data-name');
3// replace "data-name" with your data attribute name
4
5console.log(dataAttribute);
1//javascript get html data attribute
2<button data-id="1" >Click</button>
3<button data-id="2" >Click</button>
4<button data-id="3" >Click</button>
5const btns=document.querySelectorAll('button[data-id]');
6[...btns].forEach(btn => console.log(btn.getAttribute('data-id')))
1const article = document.querySelector('#electric-cars');
2
3article.dataset.columns // "3"
4article.dataset.indexNumber // "12314"
5article.dataset.parent // "cars"
1<article
2 id="voitureelectrique"
3 data-columns="3"
4 data-index-number="12314"
5 data-parent="voitures">
6...
7</article>
8var article = document.getElementById('voitureelectrique');
9
10article.dataset.columns // "3"
11article.dataset.indexNumber // "12314"
12article.dataset.parent // "voitures"
1<a data-color="blue">
2 This tag contains a user-defined color attribute
3</a>
4
5<div data-link="https://github.com/k-vernooy">
6 This tag contains a data attribute with a link
7</div>