I'll-Be-Back
I'll-Be-Back

Reputation: 10828

Pass ID into Bootstrap Modal?

How do I pass ID from <button> into Bootstrap Modal. So I can use ID to get information from ajax to display in the Modal.

For example:

Button:

<button id="123" type="button" class="btn btn-sm" data-toggle="modal" data-target="#Testing">Edit</button>

Should I use shown.bs.modal do this?

$("#Testing").on('shown.bs.modal', function () { 
  // How to get ID? id="??"
  //Ajax
});

Upvotes: 0

Views: 528

Answers (1)

Tushar
Tushar

Reputation: 87203

You can use event object that is passed to the show.bs.modal handler.

$('#Testing').on('show.bs.modal', function (event) {
    var id = $(event.relatedTarget).attr('id');
});

event.relatedTarget refers to the button that has triggered the modal open event.

Bootstrap Documentation

Upvotes: 2

Related Questions