Fernando André
Fernando André

Reputation: 1213

jquery adding to the last tr of the first table

I have a table structure like the one shown below:

<table id="result">
<tr>
<td id="groupx">
   <table>
   <tr>
   <td>Data</td>
  </tr>
</td>
</tr>
</table>

I want to add a TD to the last tr of table result. Trying x.appendTo($("#result tr:last")); isn't working since it's adding to the last tr of a table id "group"

Any sugestions on how I can keep using this structure of html and do this?

Upvotes: 0

Views: 6517

Answers (1)

Tamas Czinege
Tamas Czinege

Reputation: 121404

While Nadia's answer was almost good, it missed one thing: browsers put all direct tr childs of a table into a tbody element, so this:

<table>
  <tr>
    <td>Lol</td>
  </tr>
</table>

gets translated internally to this:

<table>
  <tbody>
    <tr>
      <td>Lol</td>
    </tr>
  </tbody>
</table>

So all you need to do is this:

x.appendTo($("#result>tbody>tr:last"));

Let me know if it works.

Upvotes: 3

Related Questions