I wanted to change the name of the class ‘popup’ to ‘popup active’ when I click some button. The code isn’t working please help me out.
html:
<!DOCTYPE html>
<html>
<body>
<script src="js.js"></script>
<button class="button">click me</button>
<div class="popup"></div>
</body>
</html>
js.js:
const but = document.querySelector('.button');
const pop = document.querySelector('.popup');
but.onclick = () => {
pop.classList.add('active');
}
2
Answers
Your script is executed before your DOM is built, place it after your elements or defer it.
OR
You could also watch for your DOM being loaded:
there is a small conflict in the place of
script
Here is a corrected code