2
Consider the following HTML:
<div id="div">
<button class="foo">Botão1</button>
</div>
And the following script:
$(".foo").click ->
alert "foobar"
$("#div").append '<button class="foo">Botão2</button>'
Rotate on Jsfiddle and check: http://jsfiddle.net/dpa8rj8L/
In Button1 the event is triggered. Not in Button2, because it was added later.
I would like to know how to make the event available also for the second button, if possible, without having to add the event "click" again.
It worked. Updated version of Jsfiddle for anyone who wants to check out: http://jsfiddle.net/dpa8rj8L/7/
– user7261
Just to complement the answer, this is delegation of events in jQuery.
– danguilherme
Thanks, @Danguilherme, I updated the answer with the link, Perfect :)
– brasofilo