Some attributes are directly accessible as properties of the element (e.g. alt
, href
, id
, title
and value
).
var a = document.querySelector("a"),
url = a.href;
Other attributes, including data-attributes can be accessed as follows:
var a = document.querySelector("a"),
tooltip = a.getAttribute("aria-label");
Data attributes can also be accessed using dataset
(modern browsers)
// <a href="#" data-tracking-number="ABC-123">Widget</a>
var a = document.querySelector("a"),
tracker = a.dataset.trackingNumber;