0

Using Jquery, after a completed ajax call, how can I say remove() this element you just clicked? Using the following code doesn't work:

$( ".deletegid" ).click(function() {   
    var imageid = $(this).attr('name');
    $.ajax({
        type: "POST",
        url: "/public/index.php/admin/content/athletes/deleteimagefromgallery",
        data: {
            imageid: imageid,
            galleryid: $("input[name=athletes_gid]").val(),
            ci_csrf_token: $("input[name=ci_csrf_token]").val()
        }
    })
    .done(function() {
        $(this).remove();           
    });
});
pomeh
  • 4,742
  • 4
  • 23
  • 44
LazyPeon
  • 339
  • 1
  • 19

3 Answers3

4

Use context option of ajax to set context to current element:

$.ajax({
    type: "POST",
    context:this,
    url: "/public/index.php/admin/content/athletes/deleteimagefromgallery",
    data: { imageid: imageid ,galleryid: $("input[name=athletes_gid]").val(), ci_csrf_token: $("input[name=ci_csrf_token]").val() }
})
.done(function() {
    $(this).remove();           
});
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

Keep it in a variable

$(".deletegid").click(function() {
    var obj = $(this);
    var imageid = $(this).attr('name');
    $.ajax({
            type: "POST",
            url: "/public/index.php/admin/content/athletes/deleteimagefromgallery",
            data: {
                imageid: imageid,
                galleryid: $("input[name=athletes_gid]").val(),
                ci_csrf_token: $("input[name=ci_csrf_token]").val()
            }
        })
        .done(function() {
            obj.remove();
        });
});

this refers to ajax object in done event.

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
0

Use as

$(".deletegid").click(function() {
var objectControl = $(this);
var imageid = objectControl.attr('name');
$.ajax({
        type: "POST",
        url: "/public/index.php/admin/content/athletes/deleteimagefromgallery",
        data: {
            imageid: imageid,
            galleryid: $("input[name=athletes_gid]").val(),
            ci_csrf_token: $("input[name=ci_csrf_token]").val()
        }
    })
    .done(function() {
        objectControl.remove();
    });

});

Amit
  • 15,217
  • 8
  • 46
  • 68