I’m using jquery colorbox to popup user accounts in a window. I also have a button that loads more users into the page via ajax, but for some reason users loaded with ajax do not popup in colorbox window. How can I get colorbox to work with the content that was returned via ajax?
function loadMore(pageNo) {
//$("#loading").html('Loading...').show();
var url = '/search/resultsAjax';
$.get(url, {page: pageNo}, function(response) {
$("#results").append(response);
//$("#loading").hide();
});
}
$(document).ready(function() {
var currPage = 1;
$("a.next").click(function() {
loadMore(++currPage);
});
$("a.usr").colorbox({width:"960px", height:"90%", iframe:true});
});
When you bind colorbox in
$(document).ready()with this:jQuery will go search through the page, grab everything that matches
a.usr, bind colorbox to those items, and then forget all abouta.usr. jQuery won’t remember that you’re interested ina.usrso the new ones won’t be colorbox’d.The usual solution to this sort of thing is to use
.live()or.delegate()but those won’t work here as there isn’t a “colorbox” event.However, you can bind colorbox by hand easily enough. Try change
loadMoreto something more like this:You just turn the
responseHTML into a jQuery object, find all thea.usrthings inside it, bind colorbox to those, and then insert the new HTML into the DOM as usual.