dataset
You can work with these data-* attributes in the DOM via the dataset property. The dataset property returns an array of values, indexed by the custom part of the name.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<p id="textblock" class="Survey numbers" data-Survey="apple" data-sentiment="like">
This is a test.
</p>
<script>
var elem = document.getElementById("textblock");
for (var attr in elem.dataset) {
document.writeln(attr);
}
document.writeln("Value of data-Survey attr: " + elem.dataset["Survey"]);
</script>
</body>
</html>
Home
JavaScript Book
DOM
JavaScript Book
DOM
HTMLElement:
- The HTMLElement Members
- element tag name, id, direction, language, hidden disabled information
- addEventListener
- appendChild
- attributes returns a collection containing all of the attributes
- classList
- className
- cloneNode
- createElement
- createTextNode
- dataset
- getAttribute
- getElementsByTagName
- hasAttribute
- innerHTML
- insertAdjacentHTML
- insertBefore
- isSameNode
- outerHTML
- onmouseout
- onmouseover
- querySelectorAll
- removeEventListener