getElementsByTagName...

  • mk27
  • Proficient
  • Proficient
  • User avatar
  • Posts: 334

Post 3+ Months Ago

This is the first time I've used; I'm putting it in an onload() function in the hopes that I can then do something to each element via .className, etc.
Code: [ Select ]
function init() {
    var iList = document.getElementsByTagName('img');
    var len = iList.length;
    alert(len);
    for (var i=0; i<len; i++) {
        document.write(iList[i]);
    }
}
  1. function init() {
  2.     var iList = document.getElementsByTagName('img');
  3.     var len = iList.length;
  4.     alert(len);
  5.     for (var i=0; i<len; i++) {
  6.         document.write(iList[i]);
  7.     }
  8. }

What gets me is the alert returns the correct number, but then the output is:

[object HTMLImageElement]undefinedundefinedundefinedundefined

Meaning I got a "NodeList" with 5 objects in it but the last 4 are null or undefined. All the examples I have found use this method explicitly -- what's up?
  • mk27
  • Proficient
  • Proficient
  • User avatar
  • Posts: 334

Post 3+ Months Ago

Hmmm...looks like I can't do that onload(); I added the function to the bottom of the page in <script> tags and now it works. Kind of a pain in the ...... #$!$%

Why would it return an array of the proper length with null items? I just wasted close to an hour with this... :cry:

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 49 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.