Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old November 4th, 2005, 07:05 PM
Sugapablo
Guest
 
Posts: n/a
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, 07:05 PM
Martin Honnen
Guest
 
Posts: n/a

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, 07:25 PM
Richard Cornford
Guest
 
Posts: n/a

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, 07:45 PM
Sugapablo
Guest
 
Posts: n/a

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
[================================================== ===========================]

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
trying to make my "fillvalues" function work... Martin Jørgensen answers 42 March 20th, 2006 02:45 AM
New functions in .NET 2.0 ??? Federico G. Babelis answers 6 November 22nd, 2005 10:07 PM
Search for multiple things in a string tshad answers 32 November 17th, 2005 10:18 AM
New functions in .NET 2.0 ??? Federico G. Babelis answers 7 July 22nd, 2005 01:10 AM