Add handler to Ajax loaded content
Hi all,
I'm trying to bind an event handler to a button in a page loaded via Ajax. This procedure should apply to every page I have to load via Ajax. I have tried two things: first, I declared the 'click' event handler for the button like this, and just did my ajax request. Here's the code:
- $("#close_button").click(function(){
- alert("This should popup");
- });
- $.ajax({
- url: 'js/ajax/text.html',
- success: function (html){
- $(childDivSelector).html(html);
- },
- error: function (request, status){
- alert("Error loading:" + request.responseText);
- }
- });
The content loads a button with id #close_overlay, and thought that would work, but that was not the case. So my next attempt was to declare the 'click' event handler in the definition of the 'success' function, like this:
- $.ajax({
- url: 'js/ajax/text.html',
- success: function (html){
- $("#close_button", html).click(function(){
- alert("This should popup");
- });
- $(childDivSelector).html(html);
- },
- error: function (request, status){
- alert("Error loading:" + request.responseText);
- }
- });
There's a mention to the Livequery plugin, so I did another attempt:
- $("#close_overlay").livequery(function(){
- $(this).click(function(){
- alert("This should popup");
- });
- });
But that did not work either. Anyone has any thoughts on how to do this?
Thanks in advance and have a nice day