0

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
});
I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213
  • Possible duplicate of [Passing data to a bootstrap modal](http://stackoverflow.com/questions/10626885/passing-data-to-a-bootstrap-modal) – tmg Jul 11 '16 at 13:31

1 Answers1

2

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

Tushar
  • 85,780
  • 21
  • 159
  • 179