Abdessamad BOUTGAYOUT Abdessamad BOUTGAYOUT - 4 months ago 7
Ajax Question

Ajax doesn't work sometimes

I'm using Ajax in my web application, and here's my Ajax Jquery function :

$(".AlarmLink").click(function(){
alert('ok');
$(".page-content").empty();
$("#alarm-loader").css('display','block');
$.post($(this).attr("href"),
function(data){
var PageContent = $(data).find(".page-content");
$("#alarm-loader").css('display','none');
$(".page-content").append(PageContent.html());
});
return false;
});


and I call it from HTML link using the class="AlarmLink".

But the problem is that this works sometimes and sometimes not .

Answer

Try this. Probably you have elements added to page after listener is set.

$(document).ready(function(){
 $(document).on('click', '.AlarmLink', function() {
   alert('ok');
            $(".page-content").empty();
            $("#alarm-loader").css('display','block');
            $.post($(this).attr("href"),                       
                       function(data){
                        var PageContent = $(data).find(".page-content");
                        $("#alarm-loader").css('display','none');
                        $(".page-content").append(PageContent.html());
                    });
            return false;
  })
});
Comments