livequery doesn’t bind after a DOM insertion

Tags: , , , ,



I don’t understand why livequery doesn’t bind the event, but I have to use .click. This is just an example, which might also use the .click(), but in the real code I’m forced to use livequery. Does anyone know why livequery isn’t working?

function bind_remove(comment){
    var id = comment.attr('comment_id');    
    comment.find(".remove").livequery("click", function(e){    
        $.post("/deleteComment", {id: id}, function(response){
            comment.remove();
            comments = comments_container.find('.comment');
        });    
    });
}

$(document).ready(function(){    

    var comments_container = $('#comments_container');
    var comments = comments_container.find('.comment');

    comments.each(function(){
        bind_remove($(this));
    });
    
    $(".submit_button").livequery("click", function(e){
    $.post("/newComment", {text: textarea.val()}, function(response){                    
        comments_container.last().append($(response).fadeIn('slow',function(){                    
                comments = comments_container.find('.comment');
                bind_remove(comments.last());                            
            }));
        });
    });
});

Answer

I added a random id to the last comment, then I selected it with $(‘#myid’), not using ‘last()’. Then I bind it and started to work



Source: stackoverflow