CMartins
CMartins

Reputation: 3293

Button that open a Modal don't execute my function

I have this button for opening a modal box and I whould like to execute a function of the pressed button at the same time opening the modal. The modal is opening but the function no. Here is my code

$.each(data, function (i, item) {

  $('#fileListTable tbody').append("<tr><td>" + item.File + "</td><td>" + item.DateUpload + "</td><td>" + `
                    <a href="" target="_blank" class="btn btn-danger btn-xs"><i class="fa fa-file-pdf-o" aria-hidden="true"></i> PDF </a>
                    <a href="" target="_blank" class="btn btn-primary btn-xs"><i class="fa fa-file-pdf-o" aria-hidden="true"></i> Quest </a>

    ` + "</td > <td>" + `

      <button id="btnStatus`+ i +`" class="btn btn-primary btn-xs btn-status" data-toggle="modal" href="#static"><i class="fa fa-list-alt" aria-hidden="true"></i> Status</button>
    ` + "</td><td id='statusContainer"+ i +"'>" + item.Status + "</td><td>" + item.Comments + "</td></tr > ");


});

my function

   $('button[id^="btnStatus"]').click(function () {
    console.log('A button was clicked');   
  });

Upvotes: 0

Views: 40

Answers (1)

Amit Kumar
Amit Kumar

Reputation: 5962

You have dynamic element. So you need to attach click using on event

$('body').on('click', 'button[id^="btnStatus"]', function() {
    console.log('A button was clicked');   
});

Upvotes: 2

Related Questions