-2
var rowCount = $('.destination tr').length;
        for(var i = 1; i > rowCount; i++){
            alert(i);
            var continent = $('.destination').find('tr:nth-child('+i+')').find('td:nth-child(1)').text();
            var country = $('.destination').find('tr:nth-child('+ i +')').find('td:nth-child(2)').text();
            var city = $('.destination').find('tr:nth-child('+ i +')').find('td:nth-child(3)').text();

            //alert(rowCount[i]);

        }

This is from a code of my table i want to get the value of each td in each row like this but it is returning nothing it is not alerting i is something wrong here.any idea is a appreciated

guradio
  • 15,122
  • 3
  • 30
  • 47

2 Answers2

0

The problem is your for loop is checking for "while greater than rowCount" so it never fires even once!

That should have been:

for(var i = 1; <= rowCount; i++)

or better yet use 0-based indexing:

for(var i = 0; < rowCount; i++)

As you just want to iterate the rows and get the first 3 TDs, forget all the nth-child selectors and do it this much simpler way:

 var $rows = $('.destination tr');
 $rows.each(function(){
     var $tds = $(this).find('td');
     var continent = $td.eq(0).text();
     var country = $td.eq(1).text();
     var city = $td.eq(2).text();
     // Do something with the values
 });
Gone Coding
  • 88,305
  • 23
  • 172
  • 188
0

Actually, it's faster and more comfortable to access table rows and cells without jQuery:

var table = ...;
for (var i = 0, row; row = table.rows[i]; i++) {
   for (var j = 0, col; col = row.cells[j]; j++) {
   }
}

The assignment in the condition of the loop makes it stop when the index runs out of rows/cells.

See: How do I iterate through table rows and cells in javascript?

Community
  • 1
  • 1
Aaron Digulla
  • 297,790
  • 101
  • 558
  • 777