The problem is that after replacing innerHTML with <div id="main"> , <div id="example"> the click action no longer works.
There are several ways to solve this problem:
First and probably the best way is to use .on() :
$("#main").on('click', '#example', function(){ alert("You clicked me!"); });
This applies to #main , and therefore it doesn't matter what you do with the content inside. As events bubble up, they will be noticed on , and then the selector will determine if the handler should execute.
The second is living. The following will replace your current click with #example :
$("#example").live('click', function(){ alert("You clicked me!"); });
One of the drawbacks is that jQuery constantly monitors dom for any changes that, depending on how much you have, may begin to slow down (although it may not be noticeable on faster machines). Also .live() deprecated in 1.7.
The final option is to add the success function to the #logo click. I would recommend creating a method so that you don't have duplicate code:
var example_click = function() { alert("You clicked me!"); }; $("#example").click(example_click); $("#logo").click(function(){ $("#main").load("file.php", example_click); });
This means that every time after #main loads successfully, a click will be added to #example . But this is disgusting, since you need to call the function twice.
source share