0

I have a little big problem with my code, i have a list that load with ajax and i want to add this code for each class on that list so here is my code

$(document).ajaxComplete(function() {

  $('#contactomodal').on('hide.bs.modal', function() {
    $('#contactomodalabierto').empty();
  });
  if ($('.pflist-item-inner .pfquicklinks').parent().find('.informacion').length > 0) {} else {
    $('.pflist-item-inner .pfquicklinks').prepend('<a title="" id="" class="informacion" data-original-title="Informacion" data-toggle="modal" data-target="#contactomodal"><i class="fas fa-info"></i> Contacte</a>');
  }

  $('.informacion').each().click(function() {
    if ($(this).attr('id') === '') {
      var id = $(this).nextAll('.pfquickview').attr('data-pfitemid');
    } else {
      var id = $(this).attr('id');
    }
    var action = 'contactomodal';
    $.ajax({
      url: my_ajax_object.ajax_url,
      data: {
        id: id,
        action: action
      },
      success: function(data) {
        $('#contactomodalabierto').html(data);
        return false;
      }
    });

  });

});

The problem is when i click on .informacion class, at the 2nd or 3rd click the function repear several times and always is reloading the ajax. If i change the .ajaxComplete for .ready my code doesn't work. Any idea?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Pixysve
  • 88
  • 12
  • The logic existing inside the `ajaxComplete` method, means it will happen any time an ajax request (using jQuery) finishes. This suggests it relies on data being created by an ajax request to exist before it can work. A document ready does not provide that logical coupling. It has no idea of ajax requests or their completions. – Taplar Sep 09 '20 at 19:55
  • @Taplar you are right. I was wrong. Its fixed now. – Pixysve Sep 09 '20 at 20:19

0 Answers0