Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda