Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Changing Modal Content According to the Trigger Button in Bootstrap 4</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script> <script> $(document).ready(function(){ $("#myModal").on("show.bs.modal", function(event){ // Get the button that triggered the modal var button = $(event.relatedTarget); // Extract value from the custom data-* attribute var titleData = button.data("title"); $(this).find(".modal-title").text(titleData); }); }); </script> <style> .bs-example{ margin: 20px; } </style> </head> <body> <div class="bs-example"> <!-- Button HTML (to Trigger Modal) --> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" data-title="Feedback">Feedback</button> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" data-title="Report Error">Report Error</button> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" data-title="Contact Us">Contact Us</button> <div id="myModal" class="modal fade" tabindex="-1"> <div class="modal-dialog"> <div class="modal-content"> <form> <div class="modal-header"> <h5 class="modal-title">Modal Window</h5> <button type="button" class="close" data-dismiss="modal">×</button> </div> <div class="modal-body"> <div class="form-group"> <label class="control-label">Email:</label> <input type="text" class="form-control"> </div> <div class="form-group"> <label class="control-label">Message:</label> <textarea class="form-control"></textarea> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button> <button type="button" class="btn btn-primary">Submit</button> </div> </form> </div> </div> </div> </div> </body> </html>