Home > Software design >  Do not close the modal when reset is clicked
Do not close the modal when reset is clicked

Time:11-10

I do not want the modal to be closed when reset is clicked. It should just reset the inner form. But my code is closing the modal.

<!doctype html>
<html lang="en">

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA /3y gxIOqMEjwtxJY7qPCqsdltbNJuaOe923 mo//f6V8Qbsw3" crossorigin="anonymous"></script>
</head>

<body>
    <div  id="messagemodal" tabindex="-1">
        <div >
            <div >
                <div >
                    <h5 >Modal title</h5>
                    <button type="button"  aria-label="Close"></button>
                </div>
                <div >
                    <p>Modal body text goes here.
                    <form action="/action_page.php">
                        <label for="fname">First name:</label><br>
                        <input type="text" id="fname" name="fname" value="John"><br>
                        <label for="lname">Last name:</label><br>
                        <input type="text" id="lname" name="lname" value="Doe"><br>
                    </form>
                </div>
                <div >
                    <button type="button"  data-bs-dismiss="modal" onclick="$('form').trigger('reset');return false;">Reset</button>
                    <button type="button" >Save changes</button>
                </div>
            </div>
        </div>
    </div>
    <script>
        var modal = new bootstrap.Modal(document.getElementById('messagemodal'), {
            keyboard: false
        });
        modal.show();
    </script>
</body>

CodePudding user response:

Remove data-bs-dismiss="modal" from your reset button and add it to you close button. Note: if you want the modal to stay open, when the user clicks outside of it, you could add this data-bs-keyboard="false" data-bs-backdrop="static" to the parent div with id="messagemodal"

CodePudding user response:

While the first step is to remove the data-bs-dismiss attribute, and value, this alone doesn't reset the <form> though it does – obviously – prevent the modal being dismissed:

const modal = new bootstrap.Modal(document.getElementById('messagemodal'), {
  keyboard: false
});
modal.show();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA /3y gxIOqMEjwtxJY7qPCqsdltbNJuaOe923 mo//f6V8Qbsw3" crossorigin="anonymous"></script>

<div  id="messagemodal" tabindex="-1">
  <div >
    <div >
      <div >
        <h5 >Modal title</h5>
        <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div >
        <p>Modal body text goes here.</p>
        <form action="/action_page.php">
          <label for="fname">First name:</label><br>
          <input type="text" id="fname" name="fname" value="John"><br>
          <label for="lname">Last name:</label><br>
          <input type="text" id="lname" name="lname" value="Doe"><br>
        </form>
      </div>
      <div >
        <button type="reset" >Reset</button>
        <button type="button" >Save changes</button>
      </div>
    </div>
  </div>
</div>

Instead, you should also remove:

onclick="$('form').trigger('reset');return false;"

Because using onclick is antiquated practice and you're using jQuery, though even native JavaScript offers a better unobtrusive approach than inline event-handlers.

const modal = new bootstrap.Modal(document.getElementById('messagemodal'), {
  keyboard: false
});
modal.show();

// I'd prefer a better selector, but I'm working with what you
// have, so here we retrieve the 'reset' button and use the
// on() method to bind the anonymous function as the 
// event-handler for the 'click' event:
$('button.btn.btn-secondary').on('click', function(e){
  // here we find the closest '.modal' element:
    let modal = $(this).closest('.modal'),
      // from the .modal we find the <form> element:
        form = modal.find('form');
  // and then trigger the reset() function:
  form.trigger('reset');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA /3y gxIOqMEjwtxJY7qPCqsdltbNJuaOe923 mo//f6V8Qbsw3" crossorigin="anonymous"></script>

<div  id="messagemodal" tabindex="-1">
  <div >
    <div >
      <div >
        <h5 >Modal title</h5>
        <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div >
        <p>Modal body text goes here.</p>
        <form action="/action_page.php">
          <label for="fname">First name:</label><br>
          <input type="text" id="fname" name="fname" value="John"><br>
          <label for="lname">Last name:</label><br>
          <input type="text" id="lname" name="lname" value="Doe"><br>
        </form>
      </div>
      <div >
        <button type="reset" >Reset</button>
        <button type="button" >Save changes</button>
      </div>
    </div>
  </div>
</div>

JS Fiddle demo.

As noted, this is also easily done in native JavaScript (though admittedly you're presumably using jQuery for other reasons):

const modal = new bootstrap.Modal(document.getElementById('messagemodal'), {
  keyboard: false
});
modal.show();

// caching the resetButton:
let resetButton = document.querySelector('button.btn.btn-secondary');

// binding the anonymous function of EventTarget.addEventListener() as
// the event-handler for the 'click' event:
resetButton.addEventListener('click', function(evt) {
  // caching a reference to the clicked element:
  let button = evt.currentTarget,
      // finding the closest ancestor '.modal' element:
      modal = button.closest('.modal'),
      // finding the <form> element within the .modal element:
      form = modal.querySelector('form');
  // calling the reset() function on the <form>:
  form.reset();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA /3y gxIOqMEjwtxJY7qPCqsdltbNJuaOe923 mo//f6V8Qbsw3" crossorigin="anonymous"></script>



<div  id="messagemodal" tabindex="-1">
  <div >
    <div >
      <div >
        <h5 >Modal title</h5>
        <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div >
        <p>Modal body text goes here.</p>
        <form action="/action_page.php">
          <label for="fname">First name:</label><br>
          <input type="text" id="fname" name="fname" value="John"><br>
          <label for="lname">Last name:</label><br>
          <input type="text" id="lname" name="lname" value="Doe"><br>
        </form>
      </div>
      <div >
        <button type="button" >Reset</button>
        <button type="button" >Save changes</button>
      </div>
    </div>
  </div>
</div>

JS Fiddle demo.

Also, while this does have a detrimental effect on your layout/presentation, it's important to remember that JavaScript isn't required for reset functionality, if the reset button (<button type="reset">) is within the <form> itself, as follows:

const modal = new bootstrap.Modal(document.getElementById('messagemodal'), {
  keyboard: false
});
modal.show();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA /3y gxIOqMEjwtxJY7qPCqsdltbNJuaOe923 mo//f6V8Qbsw3" crossorigin="anonymous"></script>



<div  id="messagemodal" tabindex="-1">
  <div >
    <div >
      <div >
        <h5 >Modal title</h5>
        <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div >
        <p>Modal body text goes here.</p>
        <form action="/action_page.php">
          <label for="fname">First name:</label><br>
          <input type="text" id="fname" name="fname" value="John"><br>
          <label for="lname">Last name:</label><br>
          <input type="text" id="lname" name="lname" value="Doe"><br>
          <div >
            <button type="reset" >Reset</button>
            <button type="button" >Save changes</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

JS Fiddle demo.

References:

  • JavaScript:
    • Element.closest().
    • Element.querySelector().
    • Event.
    • EventTarget.addEventListener().
  • jQuery:
  • Related