javascript - Pass a value to bootstrap modal from a bootstrap table

Javascript - Pass a value to bootstrap modal from a bootstrap table

To pass a value from a Bootstrap table to a Bootstrap modal, you can use JavaScript/jQuery to capture the value from the table row and then set it in the modal before showing it. Here's a basic example:

Assuming you have a table like this:

<table class="table"> <thead> <tr> <th>#</th> <th>Name</th> <th>Action</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>John</td> <td><button class="btn btn-primary open-modal" data-id="1">Open Modal</button></td> </tr> <tr> <td>2</td> <td>Jane</td> <td><button class="btn btn-primary open-modal" data-id="2">Open Modal</button></td> </tr> </tbody> </table> 

And a modal like this:

<div class="modal fade" id="myModal" 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">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <!-- Display value here --> <p id="modalContent"></p> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> </div> </div> </div> </div> 

You can use jQuery to capture the value from the table row and set it in the modal:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { $('.open-modal').click(function() { // Get the value from the data-id attribute of the clicked button var id = $(this).data('id'); // Set the value in the modal $('#modalContent').text('ID: ' + id); // Show the modal $('#myModal').modal('show'); }); }); </script> 

This script listens for clicks on buttons with the class .open-modal. When a button is clicked, it retrieves the value from the data-id attribute of the button and sets it in the modal. Then, it shows the modal using Bootstrap's modal method.

Examples

  1. Passing value from Bootstrap table to modal in JavaScript:

    "JavaScript pass value from Bootstrap table to modal"

    Description: This JavaScript code snippet demonstrates how to pass a value from a Bootstrap table to a modal when a row is clicked.

    <table class="table"> <tbody> <tr data-toggle="modal" data-target="#exampleModal" data-whatever="value1"> <td>Row 1</td> </tr> <tr data-toggle="modal" data-target="#exampleModal" data-whatever="value2"> <td>Row 2</td> </tr> </tbody> </table> <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">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <input type="text" class="form-control" id="recipient-name"> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Send message</button> </div> </div> </div> </div> <script> $('#exampleModal').on('show.bs.modal', function (event) { var button = $(event.relatedTarget); var recipient = button.data('whatever'); var modal = $(this); modal.find('.modal-body input').val(recipient); }); </script> 
  2. JavaScript code to pass data from Bootstrap table to modal:

    "JavaScript pass data from Bootstrap table to modal"

    Description: This JavaScript code passes data from a Bootstrap table to a modal when a row is clicked.

    <table class="table"> <tbody> <tr> <td>Row 1</td> <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-value="value1">Open Modal</button></td> </tr> <tr> <td>Row 2</td> <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-value="value2">Open Modal</button></td> </tr> </tbody> </table> <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">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <input type="text" class="form-control" id="recipient-name"> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Send message</button> </div> </div> </div> </div> <script> $('#exampleModal').on('show.bs.modal', function (event) { var button = $(event.relatedTarget); var recipient = button.data('value'); var modal = $(this); modal.find('.modal-body input').val(recipient); }); </script> 
  3. Passing value from Bootstrap table to modal using JavaScript:

    "JavaScript pass value from Bootstrap table to modal"

    Description: This JavaScript code passes a value from a Bootstrap table row to a modal when the row is clicked.

    <table class="table"> <tbody> <tr> <td>Row 1</td> <td><button type="button" class="btn btn-primary" onclick="openModal('value1')">Open Modal</button></td> </tr> <tr> <td>Row 2</td> <td><button type="button" class="btn btn-primary" onclick="openModal('value2')">Open Modal</button></td> </tr> </tbody> </table> <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">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <input type="text" class="form-control" id="recipient-name"> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Send message</button> </div> </div> </div> </div> <script> function openModal(value) { $('#exampleModal').modal('show'); $('#recipient-name').val(value); } </script> 
  4. JavaScript function to pass value from Bootstrap table to modal:

    "JavaScript pass value to Bootstrap modal from table"

    Description: This JavaScript function passes a value from a Bootstrap table row to a modal when the row is clicked.

    <table class="table"> <tbody> <tr onclick="openModal('value1')"> <td>Row 1</td> </tr> <tr onclick="openModal('value2')"> <td>Row 2</td> </tr> </tbody> </table> <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">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <input type="text" class="form-control" id="recipient-name"> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Send message</button> </div> </div> </div> </div> <script> function openModal(value) { $('#exampleModal').modal('show'); $('#recipient-name').val(value); } </script> 
  5. JavaScript code to pass data from Bootstrap table to modal:

    "JavaScript pass data to Bootstrap modal from table"

    Description: This JavaScript code passes data from a Bootstrap table row to a modal when the row is clicked.

    <table class="table"> <tbody> <tr onclick="openModal('value1')"> <td>Row 1</td> </tr> <tr onclick="openModal('value2')"> <td>Row 2</td> </tr> </tbody> </table> <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">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <input type="text" class="form-control" id="recipient-name"> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Send message</button> </div> </div> </div> </div> <script> function openModal(value) { $('#exampleModal').modal('show'); $('#recipient-name').val(value); } </script> 
  6. Passing value to Bootstrap modal from table row using JavaScript:

    "JavaScript pass value to Bootstrap modal from table row"

    Description: This JavaScript code passes a value from a Bootstrap table row to a modal when the row is clicked.

    <table class="table"> <tbody> <tr> <td onclick="openModal('value1')">Row 1</td> </tr> <tr> <td onclick="openModal('value2')">Row 2</td> </tr> </tbody> </table> <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">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <input type="text" class="form-control" id="recipient-name"> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Send message</button> </div> </div> </div> </div> <script> function openModal(value) { $('#exampleModal').modal('show'); $('#recipient-name').val(value); } </script> 
  7. Passing value from Bootstrap table to modal with JavaScript:

    "JavaScript pass value to modal from Bootstrap table"

    Description: This JavaScript code passes a value from a Bootstrap table row to a modal when the row is clicked.

    <table class="table"> <tbody> <tr onclick="openModal('value1')"> <td>Row 1</td> </tr> <tr onclick="openModal('value2')"> <td>Row 2</td> </tr> </tbody> </table> <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">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <input type="text" class="form-control" id="recipient-name"> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Send message</button> </div> </div> </div> </div> <script> function openModal(value) { $('#exampleModal').modal('show'); $('#recipient-name').val(value); } </script> 

More Tags

jaspersoft-studio autoresize case-sensitive oracle-manageddataaccess google-search-api spring-data adodb pseudo-element java-6 nmake

More Programming Questions

More Biology Calculators

More Retirement Calculators

More Fitness-Health Calculators

More Chemical reactions Calculators