JQuery Bootstrap closed.bs.alert

1

I'm not able to run the event code "closed.bs.alert".

That is, execute a code after the total removal of a snippet of code triggered when the Alert Close button is clicked on Bootstrap.

HTML:

<button id="btn_add">
  Add
</button>
<div id="result">
  <div class="col-md-12 alert">
      <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
      <div class="panel panel-default"><div class="panel-heading">0 [fixed]</div>
  </div>
</div>

SCRIPT:

$(document).ready(function(){
    var count = 1;
    $("#btn_add").click(function(){
        var str =   '<div class="col-md-12 alert">'+
                        '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>'+
                        '<div class="panel panel-default"><div class="panel-heading">'+count+' [dynamic]</div>'+
                    '</div>';
        $("#result").html($("#result").html()+str);
        count++;
    });
    $(".alert").on('closed.bs.alert', function(){
        alert('The alert message is now closed.');
    });
});

Available code here in jsfiddle.

In this case, the fixed Bootstrap Alert works, but the dynamic one does not. That is, in dynamically created alerts I can not execute the event code "closed.bs.alert" .

It should be something simple that I can not solve, maybe I should implement something with This or create unique Ids for each dynamic alert that is created.

Note: After creating a dynamic alert, the fixed alert will also stop working for the "closed.bs.alert" event.

jsfiddle

    
asked by anonymous 04.05.2016 / 03:38

1 answer

2

Change to something like:

$(document).on('click', '.alert a.close', function() {
  alert("OK");
})

See working at jsfiddle

    
04.05.2016 / 04:17