0

I'm trying to set a html img within a button .button__image and change a hidden span to display:block; on an image error (across multiple classes, which is why I'm not using getElementById), but I'm receiving an error:

ERROR

Uncaught TypeError: document.getElementsByClassName(...).addEventListener is not a function

HTML

<img src="images/logo_image.svg" class="button__image"><span class="hidden"/>stuff</span>

JS

document.getElementsByClassName("button__image").addEventListener("error", unhideHiddenFunction);

function unhideHiddenFunction() {
    alert('image error');
    document.getElementsByClassame("hidden").style.display = "block";
}
jo_va
  • 13,504
  • 3
  • 23
  • 47
KingLouie
  • 399
  • 1
  • 5
  • 13
  • 1
    `getElementsByClassName()` returns collection, use index........`document.getElementsByClassName("button__image")[0].addEventListener....` – Mamun Feb 17 '19 at 11:46
  • I'm afraid this doesn't work. – KingLouie Feb 17 '19 at 11:56
  • What is the error after fix? You also have typo in `document.getElementsByClassame("hidden").style.display = "block";` – Mamun Feb 17 '19 at 11:59
  • Good catch on the typo, that fixed some of it. Getting this error now: `Uncaught TypeError: Cannot set property 'display' of undefined` – KingLouie Feb 17 '19 at 12:04
  • Fixed: `var elems = document.getElementsByClassName("hidden"); for (var i=0;i – KingLouie Feb 17 '19 at 12:09

0 Answers0