/    Sign up×
Community /Pin to ProfileBookmark

classList.remove() not working

Hi! I am working on a project for my girlfriend’s birthday but I’m having trouble using the classList.remove in JavaScript so that upon button press it will remove a class that in CSS will display the display to none, therefore closing the popup. I’m a programming noob so please explain as if you’re speaking to a 5 year old! 😅

HTML:

“`
<div class=”modal” id=”modal-br”>
<div class=”modal-header”>
<div class=”modal-title”>Brazil</div>
<button data-close-button class=”close-btn”>&times;</button>
</div>
<div class=”modal-body”>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Velit, accusamus.
</div>
</div>
“`

CSS:

“`
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: scale(0);
transition: 200ms ease-in-out;
border: 1px solid black;
border-radius: 10px;
z-index: 10;
background-color: white;
width: 500px;
max-width: 80%;
}

.modal.active {
transform: translate(-50%,-50%) scale(1);
}

.modal-header {
padding: 10px 15px;
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid black;
}

.modal-header .modal-title {
font-size: 1.25rem;
font-weight: bold;
}

.modal-header .close-btn {
cursor: pointer;
border: none;
background: none;
outline: none;
font-size: 1.25rem;
font-weight: bold;
}

.modal-body {
padding: 10px 15px;
}
“`

JS:

“`
var modal = document.getElementById(‘modal-br’);
const path = document.getElementById(‘BR’);
const closeBtn = document.querySelectorAll(‘[data-close-button]’);

path.addEventListener(‘click’, openModal);
closeBtn.addEventListener(‘click’, function(){

modal.classList.remove(‘active’);
overlay.classList.remove(‘active’);

});

function openModal(){
modal.classList.add(‘active’)
overlay.classList.add(‘active’)
};
“`

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@rootFeb 21.2020 — stop using const... a const is constant, not changeable, if you search the internet for javascript const you will find the information you need.
Copy linkTweet thisAlerts:
@SempervivumFeb 21.2020 — You are using querySelectorAll() in a wrong way: It returns a nodelist; you have to use querySelector() instead or get the first element in the nodelist:

`const closeBtn = document.querySelector('[data-close-button]');</C><br/>
or<br/>
<C>
const closeBtn = document.querySelectorAll('[data-close-button]')[0];`


BTW: The close button was missing in your HTML, I had to add it. And the overlay is missing either.
×

Success!

Help @frankie1243 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...