7

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?

5 Answers 5

13
$('#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/

Sign up to request clarification or add additional context in comments.

Comments

3

You can try the below jQuery

$('.tablerow').each(function(index) {
    $(this).children('td').first().addClass('class');
});

This will solve your problem :)

Comments

0
$(".tablerow").find("td:first-child").addClass('class');

Comments

0

I believe the following would work:

$('.tablerow td:first-child').addClass('class');

Comments

0
$('#tablecontent td:first-child').addClass("first-row");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.