Connecting Tech Pros Worldwide Help | Site Map

Why do numbers work, but variables don't in this array?

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 4th, 2005, 06:05 PM
Sugapablo
Guest
 
Posts: n/a
Default Why do numbers work, but variables don't in this array?

Ok, here's my dumbdumb question of the day:

I have this loop that's supposed to loop through the 11 XML elements named
"title" in the object.

When I put a number in the array brackets like this:

tables = response.getElementsByTagName("title");

for(var i = 0;i <= tables.length;i++) {
strTitles = strTitles + tables[1].firstChild.nodeValue+"<br/>";
}

document.getElementById('foo').innerHTML = strTitles;

....I get the appropriate value displayed (albeit 11 times).

But when I try to put in the variable "i", like this:

tables = response.getElementsByTagName("title");

for(var i = 0;i <= tables.length;i++) {
strTitles = strTitles + tables[i].firstChild.nodeValue+"<br/>";
}

document.getElementById('foo').innerHTML = strTitles;

....I get the following error:

Error:tables[i] has no properties?

Any clue why? I'm sure when I hear the answer I'll be kicking myself, but
I have to know. :)


--
[================================================== ===========================]
Sugapablo -> http://www.sugapablo.net
[================================================== ===========================]


  #2  
Old November 4th, 2005, 06:05 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Why do numbers work, but variables don't in this array?



Sugapablo wrote:

[color=blue]
> for(var i = 0;i <= tables.length;i++) {[/color]

for (var i = 0; i < tables.length; i++) {


--

Martin Honnen
http://JavaScript.FAQTs.com/
  #3  
Old November 4th, 2005, 06:25 PM
Richard Cornford
Guest
 
Posts: n/a
Default Re: Why do numbers work, but variables don't in this array?

Sugapablo wrote:[color=blue]
> Ok, here's my dumbdumb question of the day:
>
> I have this loop that's supposed to loop through the
> 11 XML elements named "title" in the object.
>
> When I put a number in the array brackets like this:
>
> tables = response.getElementsByTagName("title");[/color]

So there is no Array involved in this question at all then? The -
getElementsByTagName - method returns an object implementing the -
NodeList - interface.

Incidentally, it is distinctly odd to name a variable "tables" and
assign a reference to a - NodeList - of TITLE elements to that
variable.
[color=blue]
> for(var i = 0;i <= tables.length;i++) {[/color]

You have recognised that javascript tends to use zero-based Arrays,
collections, etc. That is true of the - NodeList - interface as well.
But the - length - property of such objects is the number of elements
they contain and so includes the object at index zero. You should
iterate to - i < tables.length - because there will be no element at
index tables.length.
[color=blue]
> strTitles = strTitles + tables[1].firstChild.nodeValue+"<br/>";
> }
>
> document.getElementById('foo').innerHTML = strTitles;
>
> ...I get the appropriate value displayed (albeit 11 times).[/color]

You have 11 TITLE elements in the same document?
[color=blue]
> But when I try to put in the variable "i", like this:
>
> tables = response.getElementsByTagName("title");
>
> for(var i = 0;i <= tables.length;i++) {
> strTitles = strTitles + tables[i].firstChild.nodeValue+"<br/>";
> }
>
> document.getElementById('foo').innerHTML = strTitles;
>
> ...I get the following error:
>
> Error:tables[i] has no properties?[/color]

When - i - equals the - length - of the - NodeList - there is no element
at - tables[i] -.
[color=blue]
> I'm sure when I hear the answer I'll be kicking myself, ...[/color]

Yes, you will. ;)

Richard.


  #4  
Old November 4th, 2005, 06:45 PM
Sugapablo
Guest
 
Posts: n/a
Default Re: Why do numbers work, but variables don't in this array?

On Fri, 04 Nov 2005 19:17:25 +0000, Richard Cornford wrote:
[color=blue]
> When - i - equals the - length - of the - NodeList - there is no element
> at - tables[i] -.
>[color=green]
>> I'm sure when I hear the answer I'll be kicking myself, ...[/color]
>
> Yes, you will. ;)[/color]

Yes, I am. (*explitive*)

Thanks!


--
[================================================== ===========================]
Sugapablo -> http://www.sugapablo.net
[================================================== ===========================]

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.