jquery traversing an appended to ol/ul
I'm开发者_开发问答 trying to traverse a ul that has been appended to before traversing like so:
var items = [];
$('#list').append('<li id="item_10">item 10</li>');
$('#list').each(function () {
var item = $(this).attr('id');
items.push(item);
});
alert(items.toString());
But when I alert the output I don't see the item I added. Anyone have a clue on how I can add to a list then traverse it?
You are looping over the ul
parent. Probably want something like
$('#list li').each(function() {
});
to loop over the li
nodes.
What andy said.
Also consider using jQuery awesome chainability:
var items = [];
$('#list')
.append('<li id="item_10">item 10</li>')
.find('li')
.each(function () {
var item = $(this).attr('id');
items.push(item);
});
alert(items.toString());
精彩评论