views:

29

answers:

5

Hey folks, I have a table I'm working with that is like such:

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tablecontent">
  <tr class="tablerow">
    <td>This is the td I want to add a class to.</td>
    <td class="cell2">Stuff</td>
    <td class="cell3">Stuff</td>
  </tr>
  <tr class="tablerow">
    <td>This is the td I want to add a class to.</td>
    <td class="cell2">Stuff</td>
    <td class="cell3">Stuff</td>
  </tr>
</table>

The first TD tag in each row does not have a class or ID to work with. I don't have access to change the HTML output so i figured to add in a bit of jQuery to target the first TD tag of each tablerow. How would I do this?

A: 
$('.tablerow').each(function(index) {
    $(this).children('td').first().addClass('class');
});
Scott
+5  A: 
$('#tablecontent td:first-child').addClass('someClass');

This uses the first-child selector to select all <td> elements in the #tablecontent table that are a first-child of their parent.

Example: http://jsfiddle.net/duKKC/

patrick dw
Awesome. This worked perfectly. Thanks!
Robert E
@Robert - You're welcome. :o)
patrick dw
A: 
$(".tablerow").find("td:first-child").addClass('class');
Dror
A: 

I believe the following would work:

$('.tablerow td:first-child').addClass('class');
Jeff Siver
A: 
$('#tablecontent td:first-child').addClass("first-row");
Christian