Friday 19 January 2018

php - jQuery code won't apply to dynamic content even with the ajaxComplete() function. Any suggestions?

I have a spreadsheet of data on my website, which is
loaded in from a database, and the result looks something like
this:




id="tbody">









...


First
Name
Last Name




More
content (rows) appear as the user scrolls down, and this is done with Ajax, like
so:



$(document).ready(function(){
/*Get
more clients on scroll*/
var clientCount =
33;
$(window).scroll(function(){
var leftToBottom =
$(document).height() - $(window).height() - $(window).scrollTop();
if(
leftToBottom == 0) {
clientCount = clientCount + 11;

$("#tbody").load("loadClients.php", {

clientNewCount :
clientCount
});

}
});
});


However,
each row of data has something like an edit button which opens a modal, a delete button,
a multiple-delete-selection, etc. These are all manipulated through
jQuery/JS.



This question gave me an idea of what
to do, but I just couldn't apply it to my code: href="https://stackoverflow.com/questions/4226316/running-jquery-scripts-on-ajax-loaded-content">Running
JQuery scripts on AJAX loaded
content




Someone suggested
using the ajaxComplete() function, but this means the jQuery code will only work after
the ajax content has been completed (duh).



Aside
from using the $(document).ajaxComplete() function, what can I do to apply my jQuery
code to the new ajax content?



Problem is, the
jQuery code won't work UNTIL the user scrolls down to get more content. So a user cannot
edit, delete or select any data until they scroll down all the way to fetch the new
content.



Pray, how can I apply the jQuery to
both the content BEFORE the ajax call as well as AFTER the ajax
call?



Currently I am including the jQuery script
in both documents; the original document and the loaded-content. I'm sure this isn't the
correct way to do it, but for now it is
working.




P.S. the jQuery code
contains many click events, preventDefaults, and other basic code to open and close
modals.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...