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

133
Visualizações
Modal opens and closes immediately

I am attempting to display a modal with results of a php query, after a button is submitted, but the modal flashes onto the screen, instead of remaining until it is manually closed, due to the fact that submitting a form refreshes the page.

<script>
var modal = document.getElementById("myModal");
var btn = document.getElementById("submitForm");
var span = document.getElementsByClassName("close")[0];
var form = document.getElementsByClassName("myForm")[0];

btn.addEventListener('click', openModal);
function openModal() {
    form.submit();
    modal.style.display = "block";
    console.log("shiet");
}

span.addEventListener('click', closeModal);
function closeModal(){
    modal.style.display = "none";
}

window.addEventListener('click', function(event){
  if (event.target == modal) {
    modal.style.display = "none";
  }
});
</script>

On clicking the button id=submitForm, I want a modal to pop up, containing the results of a php script. This modal can then be closed by clicking off the screen or by activating closeModal, by clicking a span element.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try this:

window.addEventListener('click', function(event){
  if (event.target != modal) {
    modal.style.display = "none";
  } else {
    modal.style.display = "block";
  }
});

Here I meant that if the user is clicking on the window but not on the modal, then hide that; else keep the modal visible...

I think the modal flashes because of this reason!

I hope this works!

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