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

70
Vistas
Modal disappearing instantly

I am a beginner, trying to implement 'click' event listener to pop up a modal. But as soon as I click the link the modal appears and disappear instantly.

const btn = document.getElementById(btn");
const modal= document.getElementById("modal");

const showModal = function (el, modalId) {
  el.addEventListener("click", function () {
    modalId.classList.remove("hidden");
  });
};

showModal(btn, modal);
.hidden{
 display: none
}
<a class='btn'> Click </a>
<div id='modal' class='hidden'> Modal Content </div>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you should define id="btn" on your a tag to be able to do document.getElementById("btn")

const btn = document.getElementById("btn");
    const modal = document.getElementById("modal");

    const showModal = function(el, modalId) {
      el.addEventListener("click", function() {
        modalId.classList.remove("hidden");
      });
    };

    showModal(btn, modal);
.hidden {
  display: none
}
<a id="btn"> Click </a>
<div id='modal' class='hidden'> Modal Content </div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Thanks You so much guys for responding. I found my mistake, I was using 'link' tag with empty 'href' attribute, due to which the page was reloading ever time I clicked it. I simply replaced the links with button tag and now it is working just fine.

Although I still want to know if it is possible to stop the page from reloading when link element is clicked.

Also, thank you for highlighting the 'class/id' typo.

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