Find out how many attributes an <img> element have:
var x = document.getElementById("myImg").attributes.length;
Click the button to see how many attributes the img element have.
<!DOCTYPE html> <html> <body> <img id="myImg" alt="Flower" src="image1.png" width="150" height="113"> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {//from w w w. j a v a 2 s.c om var x = document.getElementById("myImg").attributes.length; document.getElementById("demo").innerHTML = x; } </script> </body> </html>