views:

39

answers:

1

By this code I add comments to a posts. But there is a problem - the first comment work, but i am not able to add next. I use livequery, so it shold work. Can you help me?

$('form.comment_form').livequery('submit', function a()  
  {
      var element = $(this);        
      var wall_post_id = element.attr("id");
       var wall_message = $('#comment_text'+wall_post_id).attr('value');

        $.post("home/add_comment", $(this).serialize(), function(response)
        {
            if(response.error == "0")
            {
          $("div#comments"+wall_post_id).replaceWith(response.message);
            }
            else if(response.error == "1")
            {
               alert(response.message);
               return false;
            }
      }, "json");
      return false;
   });
+1  A: 

The problem is when you use the replaceWith it is completely removing that DOM element. So the second time it runs for the second comment there is no DIV that matches any more so there is nothing to replace. Try appending instead. See the following code:

$('form.comment_form').livequery('submit', function a()  
{
  var element = $(this);        
  var wall_post_id = element.attr("id");
  var wall_message = $('#comment_text'+wall_post_id).attr('value');

    $.post("home/add_comment", $(this).serialize(), function(response)
    {
        if(response.error == "0")
        {
         $("div#comments"+wall_post_id).append(response.message);
        }
        else if(response.error == "1")
        {
           alert(response.message);
           return false;
        }
  }, "json");
  return false;
});
Jeff Beck