Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
Event Listeners only working on page refresh

I know there are a few similar questions but I have waded through them without any luck. My event listeners (both "click") work great, but only once, after a page refresh. It is just code to slide the hidden nav menu out for a mobile device screen.

Here is the JS code:

/*Menu responsive code*/
const hamburger = document.getElementById('menuIcon');
const closeMenu = document.getElementById('closeNav');
const navUL = document.getElementById('navUL');

hamburger.addEventListener('click', () =>{
    navUL.classList.toggle('show');
});


closeMenu.addEventListener('click', () =>{
    navUL.classList.toggle('hidden');
})

Again, this problem has to do with the javascript, not the CSS/HTML since it works great that one time (unless I am completely wrong). Also new to javascript.

hamburger and closeMenu are my ionicons used/the buttons. the show and hidden classes just translateX between 0% and 100%.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your case with 2 classes you should remove the other class when you are adding the new one.

if you are using only one class you can use toogle but with 2 classes yo have to use add and remove together. exemple here :

const hamburger = document.getElementById('menuIcon');
const closeMenu = document.getElementById('closeNav');
const navUL = document.getElementById('navUL');

hamburger.addEventListener('click', () =>{
    navUL.classList.add('red');
     navUL.classList.remove('blue');
});


closeMenu.addEventListener('click', () =>{
      navUL.classList.add('blue');
      navUL.classList.remove('red');
})

https://codepen.io/pen/?editors=1111

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!