Nope, you can't use it like that. append
is an atomic operation, which creates the element directly.
// The <ul> element is added to #details, then it is selected and the jQuery
// selection is put in the "list" variable.
var list = $('<ul/>').appendTo('#details');
for (var i = 0; i < 10; i++) {
// New <li> elements are created here and added to the <ul> element.
list.append('<li>something</li>');
}
Alternatively, generate the HTML and add it all at once (this will be more similar to your original code):
var html = '<ul>';
for (var i = 0; i < 10; i++) {
html += '<li>something</li>';
}
html += '</ul>';
$('#details').append(html);
This code is noticeably faster when dealing with many elements.
If you need a reference to the list, just do the following instead of $('#details').append(html);
:
var list = $(html).appendTo('#details');
#details
element and put it inlist
. Then the<li>
elements will be added to the#details
element. You need to select the<ul>
element after creating it. – Blixt Jul 14 '09 at 15:17append()
returned the parent or child and wasn't having any luck. – chaos Jul 14 '09 at 15:19.append('</ul>')
operation. In the DOM, you don't have an object for closing tags; a single DOM object represents the whole tag and its contents. – Blixt Jul 14 '09 at 15:38