views:

38

answers:

2

Hi,

I am having some difficults selecting the proper cells in a table. I got a soccer games tables. Each table starts with ID 'game' and then the serial number, i.e: id='game122238' .

Each table has two rows. On the first row I have 5 cells. On the second one I got one team. On the third one I got the result. On the Fourth I got the second team.

I succeeded selecting all tables:

$('table[id^=game]');

But then I got stuck. How could I: A. getting all 'home teams' into one array. B. getting all results in another array. C. getting all 'away teams' into third array.

Thanks!

+1  A: 

Use children() to iterate through the tr's and td's to get the values you want. Ort another way would be:

var i = 0;
$('table[id^=game] tr td').each(function() {
 switch(i) {
  case 0: { alert("First TD:" + $(this).html()); break; }
  case 1: { alert("Second TD:" + $(this).html()); break; }
  // ...
 }
 i++;
});

Would be easier, if you've got osme example tables.

Keenora Fluffball
Thanks for your answer! It helped me to have the break through. Well I understood that in this aweful table all td's a getting 1st, 2d, 3rd and so on, and it could end up in few hundreds, so I couldn't use the exact switch case.but I have used something similar, that count the number of cells in a row and then by the relative offset determine if to get the HTML value.Thanks.
OfficeJet
A: 

You need to use the nth Child selector

$('table[id^=game] tr td:nth-Child(3)'); // would select all cells that were in the 3rd column

If you are able to provide a sample of your HTMl I might be able to give a more concrete example.

Giles