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

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

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 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