views:

196

answers:

1

I have been working on this and cannot get this iterator to work. I have the following HTML and am trying to iterate through all rows (except the first row) and extract the values in cells (td) 2 and 3 :

<div id="statsId">
   <table width="220" cellspacing="0" cellpadding="0" border="0">
      <tbody>
         <tr>
            <td width="65"/>
            <td width="90"/>
            <td width="65"/>
         </tr>
         <tr style="font-weight: bold;">
            <td align="left">$1.00</td>
            <td>
               <a href="#">UserID1</a>
            </td>
            <td>Single</td>
         </tr>
         <tr>
            <td align="left">$6.99</td>
            <td>
               <a href="#">UserID2</a>
            </td>
            <td>Multiple</td>
         </tr>
         <tr>...
     .....(snip)

I tried the following iterator to iterate through all except the first row of the table that is a child of "div#statsID" and get the values of the 2nd and 3rd cells (in the example, the first extracted cells would have values of "UserID1" and "Single"), but it doesn't work.

$('div#statsId > table tr:not(:nth-child(1))').each(function(i, ele) {
     var secondCol = $('td:nth-child(2)', ele).innerHTML
     var thirdCol= $('td:nth-child(3)', ele).text()
    .....
  });

Any suggestions on how to specify and iterate through the rows (except the first) of a table that is a child of a div would be appreciated.

+3  A: 
$("#statsId > table tbody tr:not(:first)").each ( function() {
    var secondCol = $(this).find('td:nth-child(2)').html();
    var thirdCol= $(this).find('td:nth-child(3)').text();
});

Note

  1. You can use the id selector independently. No need to use the tag name.

  2. There is no innerHTML for a jQuery object. Use html() instead

rahul
+1 but don't forget to end the thirdCol line with ';'...:)
munch
whoops, you already caught it
munch
Also, possibly start with `var $this = $(this);` and use `$this` instead?
Gausie
Referencing the columns/cells doesn't seem to be working. The "secondCol" variable is coming back "null" all of the time and "thirdCol" is coming back with an empty string.
GregH