tags:

views:

33287

answers:

6

I am trying to work out how to get the value of table cell for each row using jquery.

My table looks like this...

<table id="mytable">
<tr><th>Customer Id</th><th>Result</th></tr>
<tr><td>123</td><td></td></tr>
<tr><td>456</td><td></td></tr>
<tr><td>789</td><td></td></tr>
</table>

I basicly want to loop though the table, and get the value of the "Customer Id" column for each row.

In the code below I have worked out that i need to do this to get it looping though each row, customer not sure how to get the value of of the first cell in the row.

$('#mytable tr').each(function() {

    var cutomerId = 

}

Many Thanks!

+2  A: 
$('#mytable tr').each(function() {
  // need this to skip the first row
  if ($(this).find("td:first").legth > 0) {
    var cutomerId = $(this).find("td:first").html();
  }
});
Strelok
There's no need to skip the first row, because it contains <th>, not <td>, so their data won't be extracted
Andreas Grech
").legth should be ").length
Mark Schultheiss
+10  A: 
$('#mytable tr').each(function() {
    var customerId = $(this).find("td:first").html();    
}

What you are doing is iterating through all the trs in the table, finding the first td in the current tr in the loop, and extracting its inner html.

To select a particular cell, you can reference them with an index:

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

In the above code, I will be retrieving the value of the third row (the index is zero-based, so the first cell index would be 0)


Here's how you can do it without jQuery:

var table = document.getElementById('mytable'), 
    rows = table.getElementsByTagName('tr'),
    i, j, cells, customerId;

for (i = 0, j = rows.length; i < j; ++i) {
    cells = rows[i].getElementsByTagName('td');
    if (!cells.length) {
        continue;
    }
    customerId = cells[0].innerHTML;
}

Andreas Grech
Thanks, this was more useful to me as I don't have control on the markup of the document I wish to parse using jQuery. So being able to use "td:first", "td:last", etc was a great help.
i5m
+1  A: 

a less-jquerish approach:

$('#mytable tr').each(function() {
    if (!this.rowIndex) return; // skip first row
    var customerId = this.cells[0].innerHTML;
});

this can obviously be changed to work with not-the-first cells.

Jimmy
+21  A: 

If you can, it might be worth using a class attribute on the TD containing the customer ID so you can write:

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

Essentially this is the same as the other solutions (possibly because I copypasted), but has the advantage that you won't need to change the structure of your code if you move around the columns, or even put the customer ID into a < span >, provided you keep the class attribute with it.

By the way, I think you could do it in one selector:

$('#mytable .customerIDCell').each(function()
{
  alert($(this).html());
});

If that makes things easier

Jennifer
I would say$('#mytable td.customerIDCell').each(function(){ alert($(this).html());});but +1
Matt Briggs
:-) thanks - but what if you want to put it into a span (I may have badly formatted the span in my answer!)
Jennifer
Think this one suits my requirments the best, but the other sugestions seem viable. Many Thanks to all that answers, stackoverflow rocks, thanks to you guys!
Sean Taylor
+1  A: 

How would I select other columns other than the first?

e.g. If I want to update the text in the "Result" column what selector should I use?

Sean Taylor
use the solutions that involve adding classes to your tds
Matt Briggs
A: 

Y not you use id for that column? say that:

    <table width="300" border="1">
          <tr>
            <td>first</td>
          </tr>
          <tr>
            <td>second</td>
          </tr> 
          <tr>
            <td>blah blah</td>
            <td>blah blah</td>
            <td id="result">Where the result should occur</td>
          </tr>
    </table>


<script type="text/javascript">
        $('#result').html("The result is....");
</script>
MOH3n MOH