Vishal B
Vishal B

Reputation: 653

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();
  });
});`

Upvotes: 1

Views: 168

Answers (2)

smoksnes
smoksnes

Reputation: 10871

Instead of using .click() when binding the button you can use one(). It will only execute once.

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

Upvotes: 1

vijayP
vijayP

Reputation: 11512

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();
   });

Upvotes: 1

Related Questions