Johan
Johan

Reputation: 35194

Appending created jquery dom elements

var $td = $('<td/>');
var $tr = $('<tr/>');

$td.text('something');

<table></table>

What i want to do is something like:

$('table').append($tr + $td);

What would be the correct syntax here? Thanks

Upvotes: 0

Views: 74

Answers (2)

ipr101
ipr101

Reputation: 24236

Try this -

$('table').append($tr).find('tr:last').append($td);

That should firstly append the tr element to the table, you then need to find the the tr element within the table and append the td to that. Thanks to Jasper for pointing out that the find('tr') was required.

Demo - http://jsfiddle.net/hbzJf/1

Upvotes: 2

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107526

You could do it all at once:

$('table').append('<tr><td>something</td></tr>');

Or

$('<tr><td>something</td></tr>').appendTo('table');

Upvotes: 2

Related Questions