473,382 Members | 1,348 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,382 software developers and data experts.

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

Nov 4 '05 #1
3 1135


Sugapablo wrote:

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


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

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 4 '05 #2
Sugapablo wrote:
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");
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.
for(var i = 0;i <= tables.length;i++) {
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.
strTitles = strTitles + tables[1].firstChild.nodeValue+"<br/>";
}

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

...I get the appropriate value displayed (albeit 11 times).
You have 11 TITLE elements in the same document?
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?
When - i - equals the - length - of the - NodeList - there is no element
at - tables[i] -.
I'm sure when I hear the answer I'll be kicking myself, ...


Yes, you will. ;)

Richard.
Nov 4 '05 #3
On Fri, 04 Nov 2005 19:17:25 +0000, Richard Cornford wrote:
When - i - equals the - length - of the - NodeList - there is no element
at - tables[i] -.
I'm sure when I hear the answer I'll be kicking myself, ...


Yes, you will. ;)


Yes, I am. (*explitive*)

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

Nov 4 '05 #4

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

Similar topics

2
by: SabMan | last post by:
I understand that document.layers is no longer supported in Netscape 7.1 but I am not sure on how to fix the code so that it will work with Netscape 7.1. I understand that document.all is no...
4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
17
by: Steve Jorgensen | last post by:
If you've ever employed custom error numbers and messages in you programs, you've probably ended up with code similar to what I've ended up with in the past something like... <code> public...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
3
by: mjaaland | last post by:
Hi, I hope someone can help me out with this one! Step 1: I get a struct from unmanaged code looking like this. public unsafe struct KEY { public UInt16 idkey_len;...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.