how to get the data attribute value in javascript code example
Example 1: javascript get attribute
//HTML
//<div id="elem" data-id="4hJ3s"></div>
var elem = document.getElementById("elem");
elem.getAttribute("data-id"); // "4hJ3s"
Example 2: js get data attribute
var element = document.querySelector('.element');
var dataAttribute = element.getAttribute('data-name');
// replace "data-name" with your data attribute name
console.log(dataAttribute);
Example 3: html javascript find data attribute
//javascript get html data attribute
<button data-id="1" >Click</button>
<button data-id="2" >Click</button>
<button data-id="3" >Click</button>
const btns=document.querySelectorAll('button[data-id]');
[...btns].forEach(btn => console.log(btn.getAttribute('data-id')))
Example 4: get data attribute javascript
// <div id="element" data-name="john"></div>
const el = document.querySelector('#element')
el.dataset.name // 'john'
Example 5: data attribute html
<div data-my-custom-data="true"></div>