Javascript DOM HTML HTMLCollection Loop through every element

Introduction

Loop through every element in an HTMLCollection:

View in separate window

<!DOCTYPE html>
<html>
<body>
<p>Loop through each HTML element on this page, and write its tag name:</p>

<script>
var x, i, l;
x = document.getElementsByTagName("*");
l = x.length;/*from  w  w w  .  ja  v  a  2s.  c o m*/
for (i = 0; i < l; i++) {
  document.write(x[i].tagName + "<br>");
}
</script>

</body>
</html>



PreviousNext

Related