JaySnel
JaySnel

Reputation: 175

javascript pop up not closing

I am having a hard time understanding why this code does not close the modal. I can open just fine, and when clsoing, i can catch the console log but the rest of the code does not run. I get no error in console and feel as if I could just be overlooking something very small. Any help is much appreciated!

<style>.corporate-about-us .section h2 { border-bottom: 1px solid #ddd; } .modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0,0,0); /* Fallback color */ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ } /* Modal Content/Box */ .modal-content { background-color: #fefefe; margin: 15% auto; padding: 20px; border: 1px solid #888; width: 70%; } /* The Close Button */ .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; }</style>



    <span class="sub-heading" id="dailyq1btn" onclick="openModal('dailyq1')">Daily Question 1</button>
    <!-- The Modal -->
    <div id="dailyq1" class="modal">
      <div class="modal-content">
        <span class="close" onclick="closeModal('dailyq1')">&times;</span>
        <div class="faq-info">
            <h2 class="faq-question sub-heading">Place Holder Title</h2>
            <p>Place Holder Body</p>
        </div>
      </div>
    </div>



    <script>
        function openModal(questionId) {
            let modal = document.getElementById(questionId);
            modal.style.display = "block";
        }
        
        function closeModal(questionId) {
            console.log(questionId)
            let modal = document.getElementById(questionId);
            modal.style.display = "none";
        }
    </script>

Upvotes: 0

Views: 25

Answers (1)

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

It works fine if you fix the button DOM, you're using a <span></button>

function openModal(questionId) {
  let modal = document.getElementById(questionId);
  modal.style.display = "block";
}

function closeModal(questionId) {
  console.log(questionId)
  let modal = document.getElementById(questionId);
  modal.style.display = "none";
}
.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}


/* Modal Content/Box */

.modal-content {
  background-color: #fefefe;
  margin: 15% auto;
  padding: 20px;
  border: 1px solid #888;
  width: 70%;
}


/* The Close Button */

.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
}
<button class="sub-heading" id="dailyq1btn" onclick="openModal('dailyq1')">Daily Question 1</button>

<!-- The Modal -->
<div id="dailyq1" class="modal">
  <div class="modal-content">
    <span class="close" onclick="closeModal('dailyq1')">&times;</span>
    <div class="faq-info">
      <h2 class="faq-question sub-heading">Place Holder Title</h2>
      <p>Place Holder Body</p>
    </div>
  </div>
</div>

Upvotes: 2

Related Questions