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')))
1// <div id="element" data-name="john"></div>
2
3const el = document.querySelector('#element')
4el.dataset.name // 'john'
1/* index.html
2<div
3 id="test-div"
4 data-first-name="Tom"
5 data-last-name="Mot"
6 data-active
7></div>
8*/
9
10// Reading
11const div = document.getElementById("test-div");
12console.log(div.dataset);
13/*
14{
15 active: ""
16 firstName: "Tom"
17 lastName: "Mot"
18}
19*/
20
21// Writing
22const div = document.getElementById("test-div");
23div.dataset.test = "Hello World!";
24console.log(div.dataset.test); // Hello World
1const article = document.querySelector('#electric-cars');
2
3article.dataset.columns // "3"
4article.dataset.indexNumber // "12314"
5article.dataset.parent // "cars"