-1

I am generating some twitter bootstrap alert messages and i want to remove the ones i have read and only be left with the ones i like to investigate further.

This how i generating them

$a =  $this->session->userdata('identity');
    $ar = trim($a,",");
    $arr = explode(',', $a);
    foreach($arr as $key => $value){
    echo '<div class="cb alert alert-warning"><a href="#" class="close" data-dismiss="alert" data-array-key="'.$key.'">&times;</a><strong>Warning!</strong> '.$value.' </div>' .'<br/>';
    }

This is my jquery code

(function ($) {
$( ".close" ).on('click', function() {
alert('close clicked');
});
})(jQuery);

I know my code works because it works on jsfiddle http://jsfiddle.net/15oar2gr/

Why is my code not working for dynamically generated content?.

user3272483
  • 408
  • 3
  • 13
  • possible duplicate of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – mplungjan Jan 23 '15 at 09:45

3 Answers3

1

Try Event Delegation like following:

$( "body" ).on('click','.close', function() {
   alert('close clicked');
});
Manwal
  • 23,450
  • 12
  • 63
  • 93
1

Try this

$( document ).on('click',".close", function() {
 alert('close clicked');
});
Sreelal P Mohan
  • 309
  • 1
  • 11
1

If you change your click event to:

$(document).on('click', '.close', function () {});

it'll work.

Ben Win
  • 830
  • 8
  • 21