Shir Gans
Shir Gans

Reputation: 2027

jQuery selecting the 3rd, 4th and 5th TD in every row with ID

I am having some difficulties selecting the proper cells in a table. I have 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 have one team. On the third one, I have the result. On the Fourth, I have 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!

Upvotes: 2

Views: 10576

Answers (2)

Giles Smith
Giles Smith

Reputation: 1962

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.

Upvotes: 7

Keenora Fluffball
Keenora Fluffball

Reputation: 1686

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.

Upvotes: 1

Related Questions