Modal search_person returns nothing when changes Javascript page

Asked

Viewed 31 times

1

inserir a descrição da imagem aqui

Good morning

I’m using a paginated modal with datatable, and when I search or change page the function stops to add the person in the main page.

Javascript function

$(function() {
    $('.add').click(function(e) {
        e.preventDefault();


        var vId = $(this).parent().parent().find("#id").text();
        var vPessoa = $(this).parent().parent().find("#pessoa").text();

        //alert(vId + '/' + vPessoa)      

        $('#idforn01').attr('value', vId);
        $('#forn01').attr('value', vPessoa);
        // document.getElementById('forn01_teste').innerHTML = vPessoa;

    });
});

1 answer

2


When you change the DOM as you described it, the javascript "fails to recognize" his class .add in the "modified" elements, since he understands that it is a new element created.

You need to delegate your click to an element above and inform the element to be created, ie, look for an element FATHER that covers your datatables list and that it is not modified, and use it.

So change your code:

$('.add').click(function(e){ 
   e.preventDefault();
   ...
})

To:

$([elemento_pai]).on('click', '.add', function(e){ 
   e.preventDefault();
   ...
})
  • Tacio, thanks, but gave this error Uncaught Referenceerror: element_pai is not defined

  • This [element_pai] you must replace with a parent element in your code. I will edit my answer for better understanding

  • I got my data table id like this <script type="text/javascript"> $('#mytable01'). on('click', '.add', Function(e){ e.preventDefault(); var Vid = $(this).Parent().Parent().find("#id").text(); var vPessoa = $(this).Parent().Parent().find("#person").text(); Alert(Vid + '/' + vPessoa) }); </script>

  • Worked thanks

  • Great, I hope you got the idea. We’re here to help!

  • I got it Thank you very much.

  • Do not forget to leave as solved. Thanks!

  • Not to impose, I’m new here as I do to leave as solved?

  • At the beginning of my reply, there will be an icon like a check right in the left corner, just click on it

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.