Modal person-search returns nothing when changing page Javascript [closed]

1

Goodmorning

I'musingapagedmodalwithdatatable,andwhenIsearchorchangepagethefunctiontoaddthepersononthemainpage.

JavascriptFunction

$(function(){$('.add').click(function(e){e.preventDefault();varvId=$(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;

    });
});
    
asked by anonymous 09.11.2018 / 11:05

1 answer

2

When you change the DOM in the way you described, javascript "no longer recognizes" your .add class in "modified" elements because it understands that it is a new element created.

You need to delegate your click on an element above and inform the element to be created, that is, look for a PAI element that encompasses your list of datatables and that it is not modified, and use it.

Then, change your code:

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

To:

$([elemento_pai]).on('click', '.add', function(e){ 
   e.preventDefault();
   ...
})
    
09.11.2018 / 11:30