I have been adding new elements via jQuery in some of my projects, and I have stumbled across a bit of a cumbersome issue.
Consider the following:
$('span.claim').click(function() {
$(this).parent().html('<span class="unclaim">Unclaim</span>'):
});
$('span.unclaim').click(function() {
$(this).parent().html('<span class="claim">Claim</span>'):
});
And the following markup...
<ul>
<li><span class="claim">Claim</span></li>
<li><span class="unclaim">Unclaim</span></li>
<li><span class="claim">Claim</span></li>
<li><span class="unclaim">Unclaim</span></li>
</ul>
Unfortunately, after the initial event is fired, no subsequent events are handled.
I realize I could completely restructure and use the .bind()
function, but because of the complexity of my project this isn't really all that feasible.
How would I go about re-binding the events after the new elements are created without ditching my anonymous functions, or is it even possible?