Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!