Javascript DOM HTML Image count attributes

Introduction

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.

View in separate window

<!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  ww .j  a va 2  s.c o  m*/
  var x = document.getElementById("myImg").attributes.length;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related