I have a simple Modal (Bootstrap 4).
I wand to add some click
functionality to save
and cacel
buttons .
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria- labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<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">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" id="submit" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" id="cancel" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
how should I implement it? as far as I read in documentation (Bootstrap’s JavaScript modal plugin), there exist no button option. I also tried :
$(document).on( "click", "#submit", function() {
alert("submit click");
});
but the function call by any click and anywher (input, modal, ...).
Regards
If you only want to add event to the buttons in this model (not from other locations), just use the id selector instead of document.
$("#exampleModal").on( "click", "#submit", function() {
alert("submit click");
});
$("#exampleModal").on( "click", "#cancel", function() {
alert("cancel click");
});