Use Element.getAttribute()
to get an attribute (including data attributes) on an element. This method can also be used to manipulate other types of attributes—things like id
, tabindex
, name
, and so on.
var elem = document.querySelector('#lunch');
var sandwich = elem.getAttribute('data-sandwich');
Source
https://vanillajstoolkit.com/reference/attributes/getattribute/