Using Lightbox.js with Rails AJAX

Posted by Jeff Kupperman Mon, 23 Apr 2007 16:11:54 GMT

We couldn't figure out why the lightbox.js effects were working perfectly in parts of our site and not at all in others, until we figured out that it never worked on elements that were created through an AJAX call.

Normally when we load a page, the function "initLightbox" is called automatically, and it "loops through anchor tags looking for 'lightbox' references and applies onclick events to appropriate links."

Problem was, the anchor tags for the images in the response didn't exist when the page finished loading initially -- they only came into being after the AJAX call. The solution? Add this line at the end of each RJS file where we might need Lightbox:

page.call 'initLightbox'

Leave a comment, View comments, View trackbacks

Your Comments.

Leave your own response

Spread the word.

Jeff Kupperman supports RSS (Real Simple Syndication), and Trackbacks from other blogs.

RSS feed for this post Trackback URI

Your Reply

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.