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.

PropertiesDescriptionReturns
nameReturns the name of the attributestring
valueGets or sets the value of the attributestring
 
<!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>
  
Click to view the demo
Home 
  JavaScript Book 
    DOM  

HTMLElement:
  1. The HTMLElement Members
  2. element tag name, id, direction, language, hidden disabled information
  3. addEventListener
  4. appendChild
  5. attributes returns a collection containing all of the attributes
  6. classList
  7. className
  8. cloneNode
  9. createElement
  10. createTextNode
  11. dataset
  12. getAttribute
  13. getElementsByTagName
  14. hasAttribute
  15. innerHTML
  16. insertAdjacentHTML
  17. insertBefore
  18. isSameNode
  19. outerHTML
  20. onmouseout
  21. onmouseover
  22. querySelectorAll
  23. removeEventListener