This is because: <Sub> (adjusted) sub>
$('.whatever').click(function() { alert("ALERT!"); });
So literally:
Find all elements currently on the page that have the class ".whatever"
Foreach element in that result set, bind this function to its click event
Naturally, adding a new DOM element will not be automatically applied by click.
the best way to solve this is to create bindings during the insert phase, that is:
var x = document.createElement("span"); $(x).click(function(){ });
A warning about simple recovery of everything
If everything is done incorrectly, this can lead to undesirable effects, i.e. the number of times the event triggers something. To stop this, you may need to unlock them first to remove previous bindings.
those.
$(x).click(foo); $(x).click(bar);
to stop it you need
$(x).unbind("click"); $(x).click(foo);
in repeated order.
source share