0
for(x in tr)
{
    var td = tr[x].getElementsByTagName('td');
    if(isNaN((bids=parseInt(td[2].innerHTML))))bids=0;
}

Loop is working only for 1st iteration but in second iteration i received error td[2] is undefined whereas each Row(tr) contain eight 8 columns(td). More than 50 Tr.

I have tried a lot but so confusing. It should not be an error.

Wasim A.
  • 9,660
  • 22
  • 90
  • 120

5 Answers5

1

If tr is not an Object, there is a chance it is picking up other properties in a for...in loop. If this is the case, .getElementsByTagName would not exist and would throw an exception.

If tr is an Array, you should be using a regular for loop instead.

Related: Why is using "for...in" with array iteration a bad idea?

Community
  • 1
  • 1
jbabey
  • 45,965
  • 12
  • 71
  • 94
0

When you get errors like this, it's useful to take a look at your variables using the Web Inspector (Chrome/Safari) or Firebug (Firefox). Put a debugging breakpoint on the var td = line, and then look at the values of the variables.

It might be something to do with using a for in loop on an array, though. Change the outer loop to this and see if it works:

for (var x = 0, l = tr.length; x < l; ++x) {
  var td = tr[x].... 
nickf
  • 537,072
  • 198
  • 649
  • 721
0

My guess is that you are accessing an unintentional property of tr by not checking your for in loop.

Try:

for(x in tr)
{
    if(tr.hasOwnProperty(x)){
        var td = tr[x].getElementsByTagName('td');
        if(isNaN((bids=parseInt(td[2].innerHTML))))bids=0;
    }
}

For a quick explanation why, consider this: "toString" in tr will return true.

lbstr
  • 2,822
  • 18
  • 29
0

Sorry i found some hidden column td under some of rows tr. Check your tr and td if display=none.

Wasim A.
  • 9,660
  • 22
  • 90
  • 120
0

Simple change your code as Dasarp said.

for(x in tr)
{
    var td = x.getElementsByTagName('td');
    if(isNaN((bids=parseInt(td[2].innerHTML))))bids=0;
}
MSRS
  • 803
  • 7
  • 20