Closing Div on click outside of div

983 Views Asked by At

I'm trying to close a contact form Div when clicking outside of the div. I got it working so that it closes outside of the Div, but the form also closes when clicking the Div. Does anyone know why this could be happening? I appreciate any tips.

HTML Code:

  <div class="formPopup" id="myForm">
<main class="mainElement">
  <button class="closeBtn" onclick="closeForm()">X</button>
  <form class="contactForm" action="contact.php" method="post">
    <input type="text" name="name" placeholder="name">
    <input type="text" name="mail" placeholder="your email address">
    <input type="text" name="subject" placeholder="subject">
    <textarea class="message" name="message" placeholder="message"></textarea>
    <button type="submit" name="submit" class="btn">SEND <MAIL></MAIL></button>
  </main>

JS code:

window.addEventListener('mouseup', function(){ if (event.target != $(".mainElement") {
document.getElementById("myForm").style.display = "none";}});
0

There are 0 best solutions below