menardmam
menardmam

Reputation: 9986

specific select in jquery

I have a table with 14 table row and 3 column of data

i like to select the table, each row only the value of the #3 column

how do i count, i know the fisrt selector, the child... but how to not net

$('#tableofdata tr td).getmethethird

Upvotes: 2

Views: 859

Answers (4)

cletus
cletus

Reputation: 625007

$("#tableofdata tr td:nth-child(3)")

or simply:

$("#tableofdata tr td:last-child")

To grab the 3rd child from this, there are a number of ways:

$(this).find(":nth-child(3)");

or:

$(":nth-child(3)", this);

or simply:

$(this)[2]; // arrays are 0 indexed

Upvotes: 6

Paolo Bergantino
Paolo Bergantino

Reputation: 488374

The problem with this solution:

$('#mytable tr').each(function() {
    var customerId = $(this).find("td").eq(2).html();    
});

Is that if your table looks like this:

<table id='mytable'>
  <tr>
    <td>col1</td>
    <td>col2</td>
    <td>15</td>
  </tr>
  <tr>
    <td>col1</td>
    <td>col2</td>
    <td>16</td>
  </tr>    
</table>

It would only get the ID of the first row because of the way it is constructed. So if you wanted to get all the IDs, you would do this:

var customers = new Array();
$('#mytable tr td:nth-child(2)').each(function() {
    customers.push($(this).html()); 
});

If you wanted the specific customer id of the Nth row, you would do this:

var customerId = $('#mytable tr').eq(N).find('td').eq(2).html();

Where N would be the 0-based index of the row you want.

Upvotes: 0

menardmam
menardmam

Reputation: 9986

what about that solution :

$('#mytable tr').each(function() {
    var customerId = $(this).find("td").eq(2).html();    
}

Upvotes: 0

Eric Wendelin
Eric Wendelin

Reputation: 44349

Not certain if this will be the fastest but...

$('#tableofdata tr td + td + td')

Upvotes: 1

Related Questions