views:

105

answers:

2

HTML:

<table id="table-1">
  <tr>

    <td>
      <table>
        <tr>
          <td>content</td>
        <tr>
      </table>   
    </td>

    <td>Content</td>

  </tr>
</table>

Is it possible to addclass 'td-1' to ONLY first-level TD's of #table-1 and not all?

Thanks

A: 

Yes. Use the selector #table-1>tbody>tr>td to only get the first level cells:

$('#table-1>tbody>tr>td').addClass('td-1');
Guffa
Hm..doesn't seem to be working.
Nimbuz
Note: the > selector means direct child (A>B matches B's that are son of A, "A B" matches a B that has a parent A anywhere above it :D
CrazyJugglerDrummer
The browser adds an implicit tbody element around the table rows. I corrected the selector.
Guffa
A: 
$('#table-1 td:first').addClass('td-1');

check: http://docs.jquery.com/Selectors/first

This will add the class only to first td in first tr,

Sinan.

EDIT:

`$('#table-1>tbody>tr>td').addClass('td-1');`

it should be as above for all first level td items.

Sinan Y.
That'd pick ONLY the first TD, not all TDs of the same level, which is what I want.
Nimbuz
i misread the question then, yes it is what it will do,
Sinan Y.
$('#table-1>tbody>tr>td').addClass('td-1'); << That worked!
Nimbuz