Your code works immediately, and therefore, of course, it has access only to existing elements. Code that adds new list items starts later when the user clicks something. You will also have to connect to this process.
One way is to bind the same event and run the code from the event handler. Be sure to pin the event after .
Their code is:
$('button').live('click', function(){ $('ul').append('<li>Added item</li>'); });
Your code ( after ):
$('button').live('click', markButtons); markButtons(); function markButtons() { $('ul li:not(.marked)') .addClass("marked") .append('<b> x</b>'); }
Updated fiddle
The reason I said that your code must complete its binding after their code is because jQuery guarantees the order of calls to event handlers. When an event occurs, handlers are called in the order in which they were attached. By attaching your handler after they join them, you guarantee that your handler will be called after they do their thing.
If you are worried that the order is messy, you can always put your code a bit back:
$('button').live('click', function() { setTimeout(markButtons, 0); });
This way your code will work after all the event handlers connected to click are started.
source share