The keyword thisin your function does not refer to the element that was clicked. By default, it refers to the highest element in the DOM that will be window.
To fix this, you can use an unobtrusive event handler instead of the deprecated event attribute on*, since they work in the scope of the element that raised the event. try it:
$("tr td img").click(deleteThisRow);
function deleteThisRow() {
$(this).closest('tr').fadeOut(400, function() {
$(this).remove();
});
}
img {
width: 20px;
height: 20px;
border: 1px solid #C00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td>blah blah blah 1</td>
<td><img src="/whatever"></td>
</tr>
<tr>
<td>blah blah blah 2</td>
<td><img src="/whatever"></td>
</tr>
<tr>
<td>blah blah blah 3</td>
<td><img src="/whatever"></td>
</tr>
</table>
Run codeHide result source
share