Enquire

Using jQuery Lightbox With live()

jQuery Scripts & Helpful Snippets

I’ve noticed a few people struggling with the following issue in a few forums, so I thought i’d write a blog post on how to overcome it. I looked through a few of the solutions people provided, but none of them made much sense to me, So I decided to tackle the issue myself.

The Problem

If you’re using a jQuery lightbox plugin and loading images dynamically onto another file with AJAX, you may have a scenario where the lightbox affect isn’t working when you click on the images. This is because the jQuery code isn’t binding with the images, because the selector is being loaded AFTER your jQuery.

The Solution

The solution is to use the live() function, with the mouseover effect. The key is the “mouseover” function- a lot of the solutions I looked at missed out the “mouseover” trigger, and mistakenly used a “click” trigger instead.

1
2
3
4
5
6
7
<script type="text/javascript">
$(function() {
 $('.selector').live('mouseover', function(){
 jQuery(this).lightBox();
 });
});
</script>

Please let me know if this helps you resolve your problem 🙂

Comments

  1. Ramilito spoke on 09/09/2012

    Thx dude, worked like a charm!

  2. codez spoke on 26/10/2012

    And much rejoicing was had! Thank you sir – saved me an hour.

  3. webinmd spoke on 07/05/2013

    thank you!!!

Leave a reply

Your email address will not be published. Required fields are marked *

X