Bootstrap Modal Confirmation

13,798

Can do it with Bootstrap Modal event listener

Add data attribute data-id to modal trigger button

<td><button type="button" data-id="jsmith22" data-toggle="modal" data-target="#removeUser" class="btn btn-default btn-lg btn-block roster-button active">Remove</button></td>

Add input type="hidden" to modal and pass the id value to modal hidden input when shown

Hidden Input

<input type="hidden" id="RowId" value="">

Modal event show script

$(document).ready(function(){
    $('#removeUser').on('show.bs.modal', function (e) {
        var id = $(e.relatedTarget).data('id');
        $('#RowId').val(id);
     });
});

Now with click event

$('#remove-button').click(function() {
        var delid = $('#RowId').val();
        //Do what ever you like to do
        $.post('/API/removeUser', {} );
});

Fiddle Example


Alternate Solution

You can skip the hidden input and create a global variable

Modal trigger button with data attribute data-id to modal trigger button

<td><button type="button" data-id="jsmith22" data-toggle="modal" data-target="#removeUser" class="btn btn-default btn-lg btn-block roster-button active">Remove</button></td>

Modal Event, Click function with Global variable script

$(document).ready(function() {
  var delid = ''; //global variable
  $('#removeUser').on('show.bs.modal', function(e) {
    delid = $(e.relatedTarget).data('id'); //fetch value of `data-id` attribute load it to global variable
    alert(delid);
  });

  $('#remove-button').click(function() {
    alert(delid); //Use the global variable here to del the record
    //Do what ever you like to do
    //$.post('/API/removeUser', {} );
  });
});

Alternate Solution Example

Share:
13,798
kernelpanic
Author by

kernelpanic

Updated on June 06, 2022

Comments

  • kernelpanic
    kernelpanic about 2 years

    I've got a table that displays user details per row along with a Remove button that launches a Bootstrap modal confirmation dialog box.

    My goal is to have the confirmation button trigger an event which will delete that particular user.

    How would I pass jsmith22 from the table row into my Javascript function?

    HTML Table

    <tr>
        <td>jsmith22</td>
        <td>John Smith</td>
        <td>555-555-5555</td>
        <td>[email protected]</td>
        <td><button type="button" class="btn btn-default btn-lg btn-block roster-button active" data-toggle="modal" data-target="#removeUser">Remove</button></td>
    </tr>
    

    Modal dialog

    <div aria-labelledby="myModalLabel" class="modal fade" id="removeUser"
    role="dialog" tabindex="-1">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">Remove Employee</h4>
                </div>
                <div class="modal-body">
                    <p>Are you sure you wish to remove this user?</p>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" data-dismiss="modal" type="button">Cancel</button>
                    <button class="btn btn-danger" id="remove-button" type="submit">Remove</button>
                </div>
            </div><!-- end modal-content -->
        </div><!-- end modal-dialog -->
    </div><!-- end modal -->
    

    Javascript

    // Remove button event trigger
    $('#remove-button').click(function() {
            $.post('/API/removeUser', {} );
    });