View Full Version : Trouble with variables in a for loop

Feb 14th, 2011, 07:39 PM
I'm using a loop to get the attributes of a series of <a> tags in an xml. Here's the code:

function getAttributes(){
for(var i=0;i <= totalSteps;i++){
whichLink = xmlDoc.getElementsByTagName("xml")[0].getElementsByTagName("a")[i].attributes.getNamedItem("href").value.split("?");
alert("correctLink detected");
myMessage += whichLink[0];

When I run the function, I get a "Object required" error for the getElementsByTagName("a")[i] line, and displayMessage() won't fire. The weird thing is, the alert(whichLink); and alert("correctLink detected"); commands both work correctly, and when I replace the i variable with a digit, like 1, everything works smoothly (save for the fact that it only returns one of the urls I'm looking for).

So something odd is going on with my loop variable, but I'm at a loss as to what.

Anyone have any ideas?

Appreciated as always,

Feb 14th, 2011, 08:00 PM
If I had to guess, I'd say that i just runs out of bounds at some point. There's no way for me to know what totalSteps is supposed to be, though.

Feb 14th, 2011, 08:02 PM
Whoops. Sorry, total steps is defined elsewhere in the code. It's usually no more than 10.

Logic Ali
Feb 14th, 2011, 08:12 PM
totalSteps looks like the determined number of elements, so use for( var i=0;i < totalSteps;i++ )

Feb 14th, 2011, 08:15 PM
After playing with the endvalue, it does appears that by using i <= totalSteps, it was exceeding the totalSteps value by one.

Strangely, if I use i == totalSteps, it ignores the loop entirely, so I had to use i <= totalSteps-1 to get it to work.

But, that was the issue. Thanks for setting me straight yet again venegal!


Feb 14th, 2011, 08:22 PM
That the loop won't run with i == totalSteps is not strange at all. The loop will run as long as that condition evaluates to true. Since i initially equals 0 (and thus doesn't equal totalSteps), the condition evaluates to false from the get-go, so the loop will never run.

Also, if totalSteps is the total number of times the loop should run, Logic Ali's suggestion (i < totalSteps) is certainly easier to read and more common that than the (a bit clumsy) i <= totalSteps - 1.

Feb 14th, 2011, 08:55 PM
Okay, I'm still seeing issues with this (oh, and sorry, I didn't see your post Logic Ali - that is simpler than what I was doing).

So totalSteps is the total number of times the loop should run. In the test page I'm working with totalSteps is 10. However, when I use (i < totalSteps), it returns only nine links. (i <= totalSteps) seems to push i out of bounds.

That shouldn't be the case, though, right?

I'm thinking there's something else in my code (something in the other code I didn't post because I didn't think it relevant) that's causing an extra loop to be needed, while putting my variable out of bounds (there's another snip of code that looks for the page's only non-link text. I think it has to do with that).

At least this thread has helped me diagnose that much - I was at a total loss before.

Thanks again, guys.