Barney Barney - 5 months ago 33
jQuery Question

a.nodeName is undefined Jquery error


a.nodeName is undefined


I've looked this up but the explanations didn't seem at all clear to me.

function deleteThisRow() {
$(this).closest('tr').fadeOut(400, function(){
$(this).remove();
});
}


<tr>
<td>blah blah blah</td>
<td>
<img src="/whatever" onClick="deleteThisRow()">
</td>
</tr>

Answer

The this keyword in your function does not refer to the element which was clicked on. By default it would refer to the highest element in the DOM, which would be the window.

Try this:

<tr>
    <td>blah blah blah</td>
    <td><img src="/whatever"></td>
</tr>
$("tr td img").click(deleteThisRow);

function deleteThisRow() {
    $(this).closest('tr').fadeOut(400, function() {
        $(this).remove();
    });
}