Search code examples
jqueryajaxcallbackslimbox

JQuery slimbox rebind after ajax callback


I'm having trouble rebinding slimbox2 after ajax content is loaded. I realize I need to rebind the function on the ajax load but I have no idea how to do that. I'm using this code to generate the external content.

$(document).ready(function() {

$('.content_box').hide();
$('.sf-menuUP a').click(function(){
$('.content_box').fadeIn('slow');
});

var hash = window.location.hash.substr(1);
var href = $('.sf-menuUP li a').each(function(){
    var href = $(this).attr('href');
    if(hash==href.substr(0,href.length-5)){
        var toLoad = hash+'.html #content';
        $('#content').load(toLoad)
    }                                           
});

$('.sf-menuUP li a').click(function(){

    var toLoad = $(this).attr('href')+' #content';
    $('#content').fadeOut('fast',loadContent);
    $('#load').remove();
    $('#wrapper').append('<span id="load">LOADING...</span>');
    $('#load').fadeIn('normal');
    window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
    function loadContent() {
        $('#content').load(toLoad,'',showNewContent())
    }
    function showNewContent() {
        $('#content').fadeIn('normal',hideLoader());
    }
    function hideLoader() {
        $('#load').fadeOut('normal');
    }
    return false;
});

});

I tried adding

    // AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED)
jQuery(function($) {
    $("a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el) {
        return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    });
});

to the bottom of my script hoping that would just reload it. But no luck. I've read livequery could work, but I don't know how/where/what to do with that.

I'm getting desperate. Please anyone?

Thanks.


Solution

  • I took a look at liveQuery for you. Here's what you'd need to do:

    $("a[rel^='lightbox']").livequery(function(){ 
        $(this).slimbox({/* Put custom options here */}, null, function(el) {
                return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
        }), function() {
            //remove slimbox? this is called when elements no longer match
        }
    });