0

The purpose of the whole code lines below is to show the hidden modal boxes when one of three buttons show-modal 1, show-modal 2, and show-modal 3 is clicked.

I try to use the code line 'modal.classList.remove('hidden')' that is stored in a function to delete the class selector 'hidden' but it does not work. Someone helps me please!

HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="style.css" />
    <title>Modal window</title>
  </head>
  <body>
    <button class="show-modal">Show modal 1</button>
    <button class="show-modal">Show modal 2</button>
    <button class="show-modal">Show modal 3</button>

    <div class="modal hidden">
      <button class="close-modal">&times;</button>
      <h1>I'm a modal window </h1>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
        veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
        commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
        velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
        occaecat cupidatat non proident, sunt in culpa qui officia deserunt
        mollit anim id est laborum.
      </p>
    </div>
    <div class="overlay hidden"></div>

    <script src="script.js"></script>
  </body>
</html>

Javascript:

'use strict';
const showModal = document.querySelectorAll('.show-modal');
const modal = document.querySelectorAll('.modal');
const overLay = document.querySelectorAll('.show-modal');
const closeModal = document.querySelectorAll('.show-modal');

for (let i = 0; i < showModal.length; i++) {
  showModal[i].addEventListener('click', function () {
    modal.classList.remove('hidden');
  });
}
  • 1
    `modal` is a collection of elements (document.querySelectorAll), not a single element, so it doesn't have a classList property. – James Sep 06 '22 at 18:59
  • You want `modal[i].classList` – Barmar Sep 06 '22 at 19:00
  • *"but it does not work"* - What it **does** do is produce an error on your browser's development console. If you're not looking there, you should start. – David Sep 06 '22 at 19:02
  • Consider to use code snippet feature to post a minimal reproducible example concerning your coding errors/problems – hoangfin Sep 06 '22 at 19:04
  • thank you all! I identified the mistake actually deriving from the querySelectorAll. I changed it to querySelector and the problem was solved. Thanks all! – Thành Nhân Sep 06 '22 at 19:07

0 Answers0