views:

65

answers:

1

In the following code $j is the jquery object. I was wondering why I can append to the tbody tag fine, however I cant to the tr tag via either of the selectiors: tr tbody or tr. How can I append to the table row correctly? Thank you for your help.

var $this = $j('<table><thead></thead><tbody><tr></tr></tbody></table>');
$j.each(settings.columns, function(i, val) {
        $j('<td></td>').appendTo($this.children('tbody tr')); 
        // Doesnt work with tr selector either, but works with tbody
        // More code
}
+3  A: 

children only finds immediate children (thus why <tbody> works), you want find otherwise:

$j('<td></td>').appendTo($this.find('tbody tr'));

You could also use $this as the context of the query:

$j('<td></td>').appendTo($j('tbody tr', $this));
Paolo Bergantino
Exactly what I was looking for, thank you.