Toggle Bootstrap Modal


Use the .modal(“toggle”) method in Bootstrap to toggle the modal.

On button click, the modal generates using the modal(“toggle”) method −

$(document).ready(function(){
  $("#button1").click(function(){
    $("#newModal").modal("toggle");
  });
});

The button is set here using the following code snippet −

<button type="button" class="btn btn-default btn-lg" id="button1">
  Modal One
</button>

You can try to run the following code to implement the modal(“toggle”) method −

Example

Live Demo

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  </head>
<body>
  <div class="container">
    <h2>Sample</h2>
    <button type="button" class="btn btn-default btn-lg" id="button1">Modal One</button>
    <div class="modal fade" id="newModal" role="dialog">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h4 class="modal-title">Sample Modal</h4>
          </div>
        <div class="modal-body">
          <p>Click outside to close it.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
        </div>
      </div>
   </div>
 </div>
</div>

<script>
  $(document).ready(function(){
    $("#button1").click(function(){
      $("#newModal").modal("toggle");
    });
  });
</script>

</body>
</html>

Updated on: 18-Jun-2020

340 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements