-2

When the user clicks on the like button I want to show a notification above "Mijn verhalen" to let the user know it liked a certain thing. I don't know what's wrong because there are no errors in my JavaScript. But the notification doesn't show up...

This is the JavaScript code:

var een =
  document.getElementsByClassName('like');

var popup =
  document.getElementsByClassName('toevoegen');

een.addEventListener('click', function() {
  popup.classList.toggle('toevoegen');
});
img.toevoegen {
  position: absolute;
  right: 5em;
  top: 4em;
  width: 10em;
}
<button class="like"></button>

<img class="toevoegen" src="images/een.png">
Barmar
  • 741,623
  • 53
  • 500
  • 612
Teddy Smit
  • 7
  • 1
  • 2

1 Answers1

0

var popup = document.getElementsByClassName('toevoegen');

document.getElementsByClassName returns HTMLCollection which is nothing but array. classList is not a known property of HTMLCollection.

You need to get the first element of this HTML collection and use the classList property. First element is nothing but the popup element.

Or else provide ID to the img tag and access it using document.getElementById which returns a DOM element not an HTML array collection.

var een =
  document.getElementsByClassName('like');

// get the first pop up element here
var popup =
  document.getElementsByClassName('toevoegen')[0];

een.addEventListener('click', function() {
  popup.classList.toggle('toevoegen');
});
prathameshk73
  • 1,048
  • 1
  • 5
  • 16