0

Hello people here is my code below...

            #ajax code after success
            $.each(obj,function(index,Object) {
                ban_list += '<div class="ban_n" id='+Object.To+'>remove ban</div>';

            })

        $('.b_list').html(ban_list).toString();

above code works fine by creating a div with the class=ban_n and id=get1_name get1_name is nothing but Object.To now i have to set onclick for ban_n

$('.ban_n').on('click',function(){
console.log($(this).attr('id'));
})

why is the above code does not work.. anyway to fix this??

Friend
  • 1,326
  • 11
  • 38
  • 62

2 Answers2

0

Use event delegation method since you have dynamically added that div:

$(document).on('click','.ban_n',function(){
console.log($(this).attr('id'));
})

learn more about event delegation here

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

I think you have to use delegation

$('.b_list').on('click','.ban_n',function(){
   console.log($(this).attr('id'));
})
Mritunjay
  • 25,338
  • 7
  • 55
  • 68