Context
I have a <div id='parent'>
and I am changing its content using $('#parent').html('my new html text')
repeatedly. Within the div I have child nodes and I attach jQuery event handlers to their click event. However after changing the children the handlers are on the old elements, so I have to attach again.
I am concerned some resource wasting effect, does the old event handlers lock any resources? (or should I unattach the handlers before I change the parent content?)
Very similar issue, but more complex: Say I utilize a jQuery plugin on the children. Normal case I write something like this:
$("<my selector which selects multiple children>").TouchSpin({
// does not matter
});
However after the content changes, I have to re-apply the plugin. The story is the same, do I have to concern about locked resources? If I should undo the plugin apply on the old children I do not even know how to do that.
Question
- What is the correct way to attach event handlers on dynamically replacable dom elements?
- What is the correct way to apply jQuery plugin on dynamically replacable dom elements?