attributes returns a collection containing all of the attributes
You can obtain a collection containing all of the attributes. attributes property returns an array of Attr objects.
The properties of the Attr object are described in the following table.
Properties | Description | Returns |
---|---|---|
name | Returns the name of the attribute | string |
value | Gets or sets the value of the attribute | string |
<!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");
var attrs = elem.attributes;
for (var i = 0; i < attrs.length; i++) {
document.writeln("Name: " + attrs[i].name + " Value: " + attrs[i].value);
}
attrs["data-Survey"].value = "banana";
document.writeln("Value of data-Survey attr: " + attrs["data-Survey"].value);
</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