I have a slider on homepage, i am trying to load next five posts in it on click of a button using Ajax. When i use Jquery .post to load the html of next five posts, the slider disappears.The cause being that a script called ‘slider.js’ does not work on the newly added html. I tried using .live() but it didn’t work, i can see the response from Ajax call in my console so i know tha call is working. I tried including the js file in Ajax response but it doesn’t fire even then. The js file is a long file with a lot of functions, i cannot enclose it in one function as suggested by some people on the forum. How would i reload the slider.js to make it work? Here is the ajax .post() code.
$(function() {
$('.nextfive').live('click', function() {
var data = {action: 'next_five',next: 5};
jQuery.post("<?php echo admin_url('admin-ajax.php'); ?>", data,
function(response) {
$('#navi').slideDown("slow");
$("#slidercontainer").html(response);
},"html");
});
});
Edit: check the page at http://axactsoft.com/samplewp/ and click the button on top ‘click five’. The code of files is pasted here http://pastebin.com/4iS6Ey10
What slider.js are you using? You can probably add an event listener to reinitialize the slider after getting the data from your php script. Something like this, though it will vary based on how the slider works.
Also, a couple of things about your coding style:
If you give more details about the slider you’re using, I can help with more specific code.
Good luck!
4. Other small simplifications, you don’t need to define the var data if you’re using it only once, again see my example.