0

I know there are a lot of questions on this topic but I could not find one that worked for me. I have comments listed on a blog post, and I want to be able to delete a comment without refreshing the page.

   $(document).ready(function(){    

    $('button.cm$cm_id').click(function(){

        $.post(\"/admin-location/delete.php?cid=\"+$cm_id, {
        }, function(response){
            $('#comment_$cm_id').remove();

        });
    }); 
});

And I have this as my comment box (as a reference to above)

<div class='commentbox' id='comment_$cm_id'>comment here...<div class='deletecomm'><button class='cm$cm_id deletecm'><img src='/images/pixel-vfl73.gif' alt='' /></button></div></div>

The comment is deleted from the database using my send-post.php page, but the div does not remove. If I reload the page, it is gone. Any ideas??

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
Chris Filippou
  • 377
  • 3
  • 11
  • try `$.get(\"/admin-location/delete.php?cid=\"+$cm_id, function(response) { $('#comment_\\$cm_id').remove(); });` – thecodeparadox Dec 14 '12 at 03:36
  • before the `.remove()` do `console.log($('#comment_$cm_id'))` to make sure you selected something. Just a quick JS debugging tip. (Or you can use the browser's debugger and break on that remove call, but you'd probably need to make a temporary variable to see what you are selecting). – Matt Dec 14 '12 at 03:52

2 Answers2

0

You have to escape special characters with two backslashes.

$('button.cm\\$cm_id').click(...
$('#comment_\\$cm_id').remove();

reference

Ricardo Alvaro Lohmann
  • 26,031
  • 7
  • 82
  • 82
  • That doesn't seem to make a difference. The button clicked is still being sent and removing from the database without the slashes. – Chris Filippou Dec 14 '12 at 03:33
0

Try this:

$('button.cm$cm_id').click(function(){
   var self = this;
    $.post(\"/admin-location/delete.php?cid=\"+$cm_id, {
    }, function(response){
        $(self).remove();

    });
Perry
  • 11,172
  • 2
  • 27
  • 37
  • that worked great to remove the actual delete button. But I need to delete the containing div. So i tried $(self).parent().remove(); and that removed only the delete button again... – Chris Filippou Dec 14 '12 at 04:05
  • haha oke, try something like this: $(div.comment_$cm_id).remove(); btw use double qoutes in html :) – Perry Dec 14 '12 at 04:07