JosephConrad
JosephConrad

Reputation: 688

Not to remove object without confirmation

I try to remove elements from my table in my django project, but I would like to have such a removal to be confirmed. Using this article http://www.developmentwall.com/delete-row-from-html-table-jquery/ I wrote such function:

<script>
function deleteAjax(row_id){
    $.ajax({
        url: "delete_item/"+ row_id +"/",
        type: "POST",
        data: {'id':row_id},
        success: function (){
            if(!confirm('Are you sure you want to delete?')){
                ev.preventDefault();
                return false;
            }else{
                $('#my_row_'+row_id).remove();
            }
        }
    });
}
</script>

Unfortunately, despite of the fact that I click that I won't to delete object, the object is removed.

Have you any hints? I suppose that it can be something wrong with ev, but I am fairly new to javascript and ajax and I do not know how to cope with this.

Upvotes: 0

Views: 80

Answers (1)

woot
woot

Reputation: 3731

try this:

<script>
function deleteAjax(row_id){
    if (!confirm('Are you sure you want to delete?')) { return; }
    $.ajax({
        url: "delete_item/"+ row_id +"/",
        type: "POST",
        data: {'id':row_id},
        success: function (){
            $('#my_row_'+row_id).remove();
        }
    });
}
</script>

Upvotes: 2

Related Questions