Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery closest not finding class

Tags:

jquery

I have the following structure which has several rows like:

<tr class="tableRow">
    <td>blah</td>
    <td>blah</td>
    <td><button id="13" class="deleteMailshot" type="button"></button></td>
</tr>

I want to perform an ajax call to delete the mailshot chosen then remove the row.

I'm using .closest() but it's not finding it... here's what I'm trying:

$.ajax({
    type: "POST",
    url: '/ajax/actions/deleteMailshot.php',
    data: {mailshot_id:mailshot_id},
    success:  function(data) {
        $(this).closest('.tableRow').fadeOut();
    }
});

I've also tried:

$(this).parent().parent('.tableRow').fadeOut();
like image 224
StudioTime Avatar asked May 10 '26 01:05

StudioTime


2 Answers

Assign this to a variable before the AJAX. This refers to something else inside the anonymous function on success.

E.g.

var $button = $(this);

Then use:

$button.closest...
like image 142
Nathan Dawson Avatar answered May 12 '26 07:05

Nathan Dawson


Try this

var self = $(this);
$.ajax({
  ....
  success:  function(data) {
    self.closest('.tableRow').fadeOut(); // use self