The attributes property returns a collection of the specified node's attributes, as a NamedNodeMap object.
Find out how many attributes a <button> element have:
var x = document.getElementById("myBtn").attributes.length;
Click the button to see how many attributes the button element have.
<!DOCTYPE html> <html> <body> <button id="myBtn" onclick="myFunction()">Test</button> <p>The result should be 2 (the button element's id and onclick attributes).</p> <p id="demo"></p> <script> function myFunction() {//from ww w.j a va 2 s .com var x = document.getElementById("myBtn").attributes.length; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The nodes can be accessed by index numbers, and the index starts at 0.
The attributes property returns a NamedNodeMap object representing a collection of node's attributes.