473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Element.getElementsByTagName weirdness

Here's my code:
===
var content = document.getElementById('content');
var collection = content.getElementsByTagName('div');

for (var i = 0; i < collection.length; i++) {
alert(collection[collection.length - i - 1].id);
}
//Replace the above loop with this one, then run again
for (var i = 0; i < collection.length; i++) {
alert(collection[i].id);
}
===
The first for-loop displays the id of every div in the collection. The
second for-loop displays the id of each div as being 'undefined'.

Ultimately, I want to change a style of the div that matches a certain
ID, but the objects in 'collection' don't seem to have any attributes
except 'id'.

Why does the loop work backwards but not forwards? And why aren't the
objects in the collection typical elements? I've tested this code in
both Firefox and IE6. Both exhibit the same behavior.

Aug 11 '06 #1
4 1820
logiczero wrote:
<snip>
Why does the loop work backwards but not forwards? And why aren't the
objects in the collection typical elements? I've tested this code in
both Firefox and IE6. Both exhibit the same behavior.
I just pasted your code into a test file, along with a couple divs, as
shown below. It works fine for me in Firefox and IE6. Must be
something else strange going on.

Rob
<div id='content'>
<div id='div-1'></div>
<div id='div-2'></div>
<div id='div-3'></div>
</div>

<script>

var content = document.getElementById('content');
var collection = content.getElementsByTagName('div');

for (var i = 0; i < collection.length; i++) {
alert(collection[collection.length - i - 1].id);
}

//Replace the above loop with this one, then run again
for (var i = 0; i < collection.length; i++) {
alert(collection[i].id);
}

</script>

Aug 11 '06 #2
Well, I think I've come up with my own answer...hope it helps someone
else.

getElementsByTagName returns a NodeList. If you want to get an item
(a.k.a element) out of that list, you have to use the NodeList.item( )
method. So in my case
===
for (var i = 0; i < collection.length; i++) {
var elem = collection.item(i);
alert(elem.getAttribute("id"));
}
===
Now elem behaves just like you would expect an element to behave.

I guess using square brackets on a NodeList (collection[i]) is
incorrect.

Aug 11 '06 #3
logiczero wrote:
Well, I think I've come up with my own answer...hope it
helps someone else.

getElementsByTagName returns a NodeList. If you want to get
an item (a.k.a element) out of that list, you have to use
the NodeList.item( ) method. So in my case
===
for (var i = 0; i < collection.length; i++) {
var elem = collection.item(i);
alert(elem.getAttribute("id"));
}
===
Now elem behaves just like you would expect an element
to behave.

I guess using square brackets on a NodeList (collection[i])
is incorrect.
The ECMAScript bindings specifically require DOM implementations to map
square bracket access to the - item - method of NodeList objects, and to
date nobody has suggested any actual issues with bracket notation
operating on NodeLists. If there really was an issue it would have been
seen before, and the use of - getElementsByTagName - is so common that
it would be well known by now.

If you want an explanation of the phenomenon you observed you have to
provide a full test case that demonstrates it. That means (minimal) HTML
and script.

Richard.
Aug 11 '06 #4
Thanks to Richard and Rob for looking at this.

I'm embarrassed to say that in the course of trying to give a more
complete example of broken code, I figured out the problem was actually
due to PHP mangling the JavaScript...so ultimately a useful, if
humbling, exercise.

Thanks again for looking, and insisting that JavaScript works like it
should.

Aug 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Richard G. | last post by:
Hi All, I am newbie in XML. I am designing a dynamic menu and retrieving data from xml. I have the xml file something like this: <?xml version="1.0" ?> <ROOT>
2
by: neousr | last post by:
<record> <title>Lunar Park</title> <author>Bret Easton Ellis</author> <publisher></publisher> <year>2005</year> </record> xAuthor = x.getElementsByTagName("author").firstChild.nodeValue...
7
by: Joseph Scoccimaro | last post by:
Currently I am trying to use JavaScript within greasemonkey to dynamically put a menu at the top of each page. I am running in to trouble when I try to append a node representing a script tag to...
1
by: Brent | last post by:
I thought this would be a problem easily fixed in a simple Google search, but I'm having a hard time finding a solution. I simply need a way to set the value of an option element from an option...
6
by: cj | last post by:
How would I check to see if a element is in an xml document. Here's the document: test.xml <?xml version="1.0" encoding="UTF-8"?> <request> <key>5678</key> <type>0200</type>...
6
by: Vortexmind | last post by:
Hi all I was wondering if this is possible in Javascript. I want to make a bookmarklet. When a user launches it, it tells the user to select an element in the page (for example a textarea) with...
3
by: rlueneberg | last post by:
I want to change the color of an "a" child element inside a table cell via javascript. Is there any way to do that? Ps: there is no id assigned to child elements. I would like to discover the child...
4
by: SM | last post by:
Hello, I have an unordered list similar to this one: <ul id=list> <li onclick="addParagraph(0)">Item 1</li> <li onclick="addParagraph(1)">Item 2</li> <li onclick="addParagraph(2)">Item...
5
by: Andy Chambers | last post by:
Hi, On both Opera and Firefox, getElementsByTagName doesn't find anything apart from <optionelements inside a select element. Why is this? Here's a page that demonstrates this behaviour. I'd...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.