0

I have this code:

$("#widgetstable tr td:nth-child("+column+")").hide();

But it selects any td's that happen to be nested inside the selected td's. (And there are a couple.)

I tried $("#widgetstable > tr > td:nth-child("+column+")").hide(); but it didn't select ANYTHING.

Rafay
  • 30,950
  • 5
  • 68
  • 101

1 Answers1

4

A tbody is inserted silently when your browser parses your HTML, and the > selector means the direct child of the parent. For the following HTML this selector would work:

<style>
   td {
     background-color: blue;
   }
</style>
<table id='widgetstable'>
  <tr>
    <td>Me</td>
  </tr>
  <tr>
    <td>
      <table>
        <tr><td>Not Me</td></tr>
      </table>
    </td>
  </tr>
</table>
<script>
  $("#widgetstable > tbody > tr > td").css('background-color', 'red');
</script>

Here is a demo

Community
  • 1
  • 1
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
  • Not sure how to mark this as having answered my question... but it did. – Matthew Davis Jan 04 '13 at 21:44
  • To be safe, I think it would be a good idea to include an explicit `` in the HTML. You could also give it an id and simplify the selector to `$("#tbodyId").children("tr").children("td")`. – Beetroot-Beetroot Jan 04 '13 at 22:36