Javascript – reinitialize dom events after ajax calls

ajaxdomjavascript

I have the following problem/doubt that I hope somebody could help me with.
Let's say I have several javascript events being initialized when I load a page. In this page I allow the User to click on several action buttons (which have those events). Now, one of these buttons makes in this case an ajax call in which a successful result will make that button disapear and make another button appear (I inject this html from the ajax response). In this moment I want to add as well an event to this new button, so after I inject the html I do so. When I click in this button I want the first button to appear again and this one to disapear. Now this new button (the one that was there in the beggining) has to have again that same event! Do I need to initialize again that event ? Isn't there a way to make that autommaticaly somehow? I mean, if I have several events for all 's of a certain div, and I load with ajax another in that div, shouldn't that respond immediattly to the same events?
Thanks

Best Answer

jQuery has a live event registering facility, that will keep events correct for all current and future matches for a selector. (jQuery.com)

As for plain Javascript, yes, you'll need to register the event again.