0

Good day, I'm trying to receive this of the link in the console.

    $('.bestehende_edit').click(function(){

            console.log($(this));
        })

Maybe I need to use delegate but I don't know how. Please explain to me why it happens and what I need to do?

$.ajax({
      method: 'POST',
      dataType: 'json',
      async: true,
      url: "/" + "<?echo $admin_type?>" + "/get_kredit_history",
      data: {
        id: << ? echo $credit - > id ? >
      },
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      },
      success: function(data) {
          $('.bestehende').css('display', 'none');
          $.each(data, function(key, value) {
            $('.kredits_area').append(
              '<div class="bestehende_data" id="bestehende_data_' + value.id + '" style="margin-left:10px;">' +
              '<div class="bestehende_header"><h3 style="display:inline-block" >Kredit ' + (key + 1) + '</h3>' +
              '<a class="btn btn-default bestehende_edit" style="float:right;margin-top:-15px;color:blue;display:inline-block" href="javascript:void(0);"><i class="fa fa-pencil"></i> </a>' +
              '<a class="btn btn-default bestehende_delete" style="float:right;margin-top:-15px;color:red;display:inline-block" href="javascript:void(0);"><i class="fa fa-trash-o"></i> </a>' +
              '</div>' +
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Andrey
  • 1
  • 3
  • Yes. You have to use event delegation for newly added elements. And based on your code it seems that is what you're exactly doing, you're adding with ajax the button you attached the click event to. – Ionut Necula Jul 10 '18 at 12:27
  • Please give me example for my case if you know how – Andrey Jul 10 '18 at 12:28
  • `$('.kredits_area').on("click",".bestehende_edit",function() { .... });` – mplungjan Jul 10 '18 at 12:28
  • This question has been asked many times on SO. You can find the answer almost everywhere on the site by typing `event delegation jquery` or something like that on the search field or by clicking here https://learn.jquery.com/events/event-delegation/ – Ionut Necula Jul 10 '18 at 12:29

0 Answers0