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

121
Views
AddEventListener only being called the first time and doesn't work after

I am creating a mock website for a restaurant and in the header i am trying to create tabs that load js modules but i am only able to get it to work the first click and then it stops working. im fairly new and not sure what is causing the issue.

import { renderHomepage } from './homepage'
import { renderMenupage } from './menu'
import { renderContactpage } from './contact'

renderContactpage();

const content = document.querySelector('#content');
const homeTab = document.querySelector("#home");
const menuTab = document.querySelector('#menu');
const contactTab = document.querySelector('#contact');
const secondMenu = document.querySelector('#secondMenu');
console.log(menuTab);

const removeDOM = () => {
    content.innerHTML = ``;
}

homeTab.addEventListener('click', () => {
    removeDOM();
    renderHomepage();
});

menuTab.addEventListener('click', () => {
    removeDOM()
    renderMenupage()
});

contactTab.addEventListener('click', () => {
    removeDOM()
    renderContactpage()
});

secondMenu.addEventListener("click", () => {
    removeDOM();
    renderMenupage();
});

please let me know if i need to give more information.

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

0

You cannot add an event listener multiple times, that will cause it to fire once. To avoid this, try using onclick. For example, something like this should work:

import { renderHomepage } from './homepage';
import { renderMenupage } from './menu';
import { renderContactpage } from './contact';

renderContactpage();

const content = document.querySelector('#content');
const homeTab = document.querySelector("#home");
const menuTab = document.querySelector('#menu');
const contactTab = document.querySelector('#contact');
const secondMenu = document.querySelector('#secondMenu');
console.log(menuTab);

const removeDOM = () => {
    content.innerHTML = '';
}

homeTab.onclick = () => {
    removeDOM();
    renderHomepage();
};

menuTab.onclick = () => {
    removeDOM()
    renderMenupage()
};

contactTab.onclick = () => {
    removeDOM()
    renderContactpage()
};

secondMenu.onclick = () => {
    removeDOM();
    renderMenupage();
};

Hoped this helped!

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!