0

I am trying to send ajax request and remove the parent of popover after success the request is success but I can't access the parent of popover to remove it that is the bottom of problem

// delete work form portfolio
    $(document).on("click", "#delete-work", function(){
        var id_val= $(this).attr("data-id");
        $.ajax({
            url: "ajax/portfoliojx.php",
            type: "POST",
            data: {
                name        : "delete_work",
                id          : id_val,

            },
            dataType: "JSON"
        }).done(function(data){
            // check if responed email error
            $(this).parents(".work").remove();
        });
    })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="work">
<div class="col-md-3">
<div class="thumbnail">
<a href="">
<img src="uploads/portfolio/251442-7.jpg" title="hello" alt="hello">
</a>
<div class="caption">
<a href=""><h4 class="tite">hello</h4></a>
</div>
<hr>
<a href="?do=edit&amp;id=23" class="btn btn-default">edit <i class="fa fa-edit"></i></a>

<a tabindex="0" class="btn btn-danger pull-left" role="button" data-toggle="popover" data-trigger="focus" data-placement="top" title="" data-content="<button class='btn btn-defualt'>no</button><span class='pull-left'><button id= 'delete-work' data-id= '23' class='btn btn-danger'>yah</button></span>" data-original-title="are you sure from this ?"> delete <i class="fa fa-trash"></i></a>
</div>
</div>
</div>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • after ajax success `this` should be in ajax scope. it won't works. – Zaza Mar 17 '21 at 06:54
  • 2
    Hi , ajax is not able to identify what is `$(this)` . Instead you can store `$(this)` inside some variable i.e : `var selector = $(this)` outside ajax call and then use `selector.closest(".work").remove()` . – Swati Mar 17 '21 at 06:55
  • 1
    Hi there thanks it worked for me just fine and magic happened – Musaab Mustafa Mar 17 '21 at 07:00
  • @MusaabMustafa added some alternative code solutions as well in my answer, you can check it for reference as well – Alive to die - Anant Mar 17 '21 at 07:05

1 Answers1

0

Inside Ajax callback the scope should be Ajax so this won't be works. the below codes works for your case

$(document).on("click", "#delete-work", function(){
        var _self = this;
        var id_val= $(this).attr("data-id");
        $.ajax({
            url: "ajax/portfoliojx.php",
            type: "POST",
            data: {
                name        : "delete_work",
                id          : id_val,

            },
            dataType: "JSON"
        }).done(function(data){
            // check if responed email error
            $(_self).parents(".work").remove();
        });
    })
Zaza
  • 406
  • 9
  • 18