Keep getting error "Uncaught TypeError: Cannot read properties of null (reading 'classList')"

I'm making a js-actuated modal but I keep getting the error "Uncaught TypeError: Cannot read properties of null (reading 'classList')". This is my code:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <script src="script.js"></script> <title>Custom Modal</title> </head> <body> <button onclick="openModal()">Open Modal</button> <p class="modal-content hidden">This is a modal</p> <script> const modalcontent = document.querySelector(".modal-content"); function openModal() { modalcontent.classList.replace("hidden", "visible"); } </script> </body> </html>

How can I fix this? Thanks
Was this page helpful?