1

When I click remove button the data is removed but I can't remove the tr that is shown in table.

<tr>

<td>name</td>
<td><button id="removebutton" data-id="?">Remove</button></td>
</tr>

I want to select tr that is in the top and remove it from the ajax response.

I have tried:

$("#removebutton).on("click",function(){
  success: function(res){
  $(this).parents("tr").remove(); //this doesn't work
  }
});
lagbox
  • 48,571
  • 8
  • 72
  • 83
A4family
  • 73
  • 1
  • 10

2 Answers2

1

Try using arrow function instead. This makes sure that the scope is kept to the click event rather than the success()

$("#removebutton").on("click", function() {
  .....
  success: (res) => {
    $(this).parent().remove(); 
  }
  ....
});
Nidhin Joseph
  • 9,981
  • 4
  • 26
  • 48
0

You can use either:

$(this).parent().remove();

// or

$(this).closest('tr').remove();

Let me know if it works :)

EDIT

I am looking forward to know why I got a negative vote :)

You cannot use the SAME ID on multiple rows..

<button class="removebutton" data-id="?">Remove</button>

then this $("#removebutton). should be $(".removebutton").

And you are missing closing " above in your selector.

nakov
  • 13,938
  • 12
  • 60
  • 110