Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

open print window to print the from in ngb popup modal with bootstrap classes

  1. User

    open print window to print the from in ngb popup modal with bootstrap classes

  2. Assistant

    To open the print window for a form in a Bootstrap modal, you can apply the following code:

    <!DOCTYPE html>
    <html>
    <head>
      <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
      <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    </head>
    <body>
    
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
      Open Modal
    </button>
    
    <!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Print Form</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <!-- Your form content here -->
            <form>
              <div class="form-group">
                <label for="exampleInputName">Name</label>
                <input type="text" class="form-control" id="exampleInputName" placeholder="Enter name">
              </div>
              <div class="form-group">
                <label for="exampleInputEmail">Email</label>
                <input type="email" class="form-control" id="exampleInputEmail" placeholder="Enter email">
              </div>
              <!-- Add more form fields as needed -->
    
              <!-- Print button -->
              <button type="button" class="btn btn-primary" onclick="window.print()">Print</button>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
    
    </body>
    </html>
    

    In the above code, the Bootstrap modal is used to display the form. The form fields can be added inside the modal-body section, along with a print button. The print button has an onclick attribute that triggers the window.print() function to open the print window.

    Please note: To use the Bootstrap CSS and JavaScript dependencies, make sure to include the respective libraries via the provided CDN links in the <head> section.

  3. User

    var printContents = document.getElementById('contentToPrint').innerHTML; var originalContents = document.body.innerHTML;

        document.body.innerHTML = printContents;
    
        window.print();
    
        document.body.innerHTML = originalContents; i want code like this
    
  4. Assistant

    Certainly! Here's an example code snippet similar to what you provided:

    <!DOCTYPE html>
    <html>
    <head>
      <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
      <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
      
      <script>
      function printForm() {
        var printContents = document.getElementById('contentToPrint').innerHTML;
        var originalContents = document.body.innerHTML;
         
        document.body.innerHTML = printContents;
         
        window.print();
         
        document.body.innerHTML = originalContents;
      }
      </script>
    </head>
    <body>
    
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
      Open Modal
    </button>
    
    <!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Print Form</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body" id="contentToPrint">
            <!-- Your form content here -->
            <form>
              <div class="form-group">
                <label for="exampleInputName">Name</label>
                <input type="text" class="form-control" id="exampleInputName" placeholder="Enter name">
              </div>
              <div class="form-group">
                <label for="exampleInputEmail">Email</label>
                <input type="email" class="form-control" id="exampleInputEmail" placeholder="Enter email">
              </div>
              <!-- Add more form fields as needed -->
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" onclick="printForm()">Print</button>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
    
    </body>
    </html>
    

    In this code, the printForm() function is defined, which gets the HTML contents of the contentToPrint element, stores the original contents of the page, replaces the body content with the form content, opens the print window, and finally restores the original contents of the page. The printForm() function is called when the print button is clicked.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.