Vishal Borade Vishal Borade - 5 months ago 12
PHP Question

modal, button on modal executing many time

I am trying to send data on button on modal using jQuery, first time it is working well but when I click second time it execute twice and for third time it execute 3 times, I want to execute that button only once, what is wrong exactly?
my modal

`<div id="Modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content modal-sm">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">product</h4>
</div>
<div class="modal-body">
<label>Quantity</label>
<input type="text" class="form-control" id="qty" value="">
<label>Note:</label>
<input type="text" class="form-control" id="note" value="">
</div>
<div class="modal-footer">
<button id="" type="button" class="btn btn-primary mdismiss" data-dismiss="modal">Add</button>
</div>
</div>
</div>
</div>`


my jQuery :

`

$('#btnbtn').click(function(){
$('#Modal').modal('show');
$('#addbutton').click(function(){
alert();
});
});`

Answer

You are adding click handler multiple time whenever you are clicking on #btnbtn button. No need of attaching click event handler to #addbutton on each click. Change your code like below:

    $('#btnbtn').click(function(){
       $('#Modal').modal('show');
    });

   $('#addbutton').click(function(){
        alert();
   });