Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript loop through all elements with tagname, alert

Tags:

javascript

var all = document.getElementsByTagName("a");
for (var i=0, max=all.length; i < max; i++) {
  alert(x.innerHTML);
}

The purpose of this script is obvious: it tries to loop through all the elements with the tag name a, and alert the contents of each one.

It doesn't run right.
It works fine, with one element, it alerts it's contents, but when there are more then one, it starts echoing undefined for each.

like image 539
user43107 Avatar asked Mar 05 '13 23:03

user43107


1 Answers

you should use alert(all[i].innerHTML). x is undefined

like image 193
uoryon Avatar answered Sep 18 '22 23:09

uoryon