Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

64
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda