Closing and resetting a modal dialog

D P :

I have a modal created modal dialog that open after 5 seconds. However, after I press the save ("close-button") inside the modal it also waits 5 seconds before it closes. Whilst not a major problem, I was wondering how I would be able to close the modal automatically and reset the contents of the modal, so that next time the button is pressed the modal input boxes are empty.

var modal = document.querySelector(".modal");
var trigger = document.querySelector(".trigger");
var closeButton = document.querySelector(".close-button");


function toggleModal() {
    setTimeout(function() {
        modal.classList.toggle("show-modal");
    }, 5000)
}

trigger.addEventListener("click", toggleModal);
closeButton.addEventListener("click", toggleModal);
.modal {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    opacity: 0;
    visibility: hidden;
    transform: scale(1.1);
    transition: visibility 0s linear 0.25s, opacity 0.25s 0s, transform 0.25s;
}
.modal-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: white;
    padding: 1rem 1.5rem;
    width: 24rem;
    border-radius: 0.5rem;
}
.close-button {
    float: right;
    width: 1.5rem;
    line-height: 1.5rem;
    text-align: center;
    cursor: pointer;
    border-radius: 0.25rem;
    background-color: lightgray;
}
.close-button:hover {
    background-color: darkgray;
}
.show-modal {
    opacity: 1;
    visibility: visible;
    transform: scale(1.0);
    transition: visibility 0s linear 0s, opacity 0.25s 0s, transform 0.25s;
}
<button class="trigger">Click here </button>
<div class="modal">
    <div class="modal-content">
      
        <h1>
          <label><b>Please state your confidence with this decision (0-100%)</b></label>
          <p>
         <input class="bottomaftertrialquestions" type="number" placeholder="Type here" name="conf1d3nce"min="0" max="100" required>
          </p>
          <p></h1>
            <button type="button" class="close-button">SAVE</button>
    </div>
</div>

Matt Oestreich :

It takes five seconds to close because you are calling the same function, which contains the 5 second timeout, when the close button is clicked:

function toggleModal() {
    setTimeout(function() {
        modal.classList.toggle("show-modal");
    }, 5000)
}

trigger.addEventListener("click", toggleModal);
closeButton.addEventListener("click", toggleModal);

Using another function, without the timeout, will fix this.

function toggleModal() {
    setTimeout(function() {
        modal.classList.toggle("show-modal");
    }, 5000)
}

function closeModalNow() {
  modal.classList.toggle("show-modal");
  // clear selection
  document.querySelector('input[name="conf1d3nce"]').value = null;
}

trigger.addEventListener("click", toggleModal);
closeButton.addEventListener("click", closeModalNow);

Demo:

var modal = document.querySelector(".modal");
var trigger = document.querySelector(".trigger");
var closeButton = document.querySelector(".close-button");


function toggleModal() {
    setTimeout(function() {
        modal.classList.toggle("show-modal");
    }, 5000)
}

function closeModalNow() {
  modal.classList.toggle("show-modal");
  document.querySelector('input[name="conf1d3nce"]').value = null;
}

trigger.addEventListener("click", toggleModal);
closeButton.addEventListener("click", closeModalNow);
.modal {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    opacity: 0;
    visibility: hidden;
    transform: scale(1.1);
    transition: visibility 0s linear 0.25s, opacity 0.25s 0s, transform 0.25s;
}
.modal-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: white;
    padding: 1rem 1.5rem;
    width: 24rem;
    border-radius: 0.5rem;
}
.close-button {
    float: right;
    width: 1.5rem;
    line-height: 1.5rem;
    text-align: center;
    cursor: pointer;
    border-radius: 0.25rem;
    background-color: lightgray;
}
.close-button:hover {
    background-color: darkgray;
}
.show-modal {
    opacity: 1;
    visibility: visible;
    transform: scale(1.0);
    transition: visibility 0s linear 0s, opacity 0.25s 0s, transform 0.25s;
}
<button class="trigger">Click here </button>
<div class="modal">
    <div class="modal-content">
      
        <h1>
          <label><b>Please state your confidence with this decision (0-100%)</b></label>
          <p>
         <input class="bottomaftertrialquestions" type="number" placeholder="Type here" name="conf1d3nce"min="0" max="100" required>
          </p>
          <p></h1>
            <button type="button" class="close-button">SAVE</button>
    </div>
</div>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=32871&siteId=1