Element hides but parent() not

53 Views Asked by At

I am trying to hide the container of an image when it can't be fournd. So far this workes which hide the "image not found" icon:

document.addEventListener("DOMContentLoaded", function(event) {
   document.querySelectorAll('img').forEach(function(img){
    img.onerror = function(){
        this.style.display='none';
    };
   })
});

But when I change this line:

this.display='none';

to

this.parent().display='none';

it's not working. How can I hide the container image?

1

There are 1 best solutions below

0
On BEST ANSWER

This isn't jQuery, just good ol' DOM.

this.parentElement.style.display = 'none';