1

I am using Yii CGridView with ajax Pagination and one of the column is Link for image or inline ajax content and i want to use FancyBox to the column.

When i use pagination i lose the binding on the new element.

if i try to make it on element click i always get error if the element has fancy-box already like below

$('a.fancy-img').live('click', function () {
     $(this).fancybox({});
      return false;
});

But I always get javascript error if i click twice on the same link.

2 Answers2

0

Try

fancybox uses $(element).data() to store its element related configuration.

So check if(!$(this).data().fancybox) --> if there is no data that means no fancybox is registered to it than register fancybox

$('a.fancy-img').live('click', function () {
    if(!$(this).data().fancybox){ //or if(!$(this).data("fancybox"))
      $(this).fancybox({});
    }
      return false;
});

.live() is deperciated use .on()

Syntax

$( elements ).on( events, selector, data, handler );
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0

The problem you are trying to initialize fancybox for the element twice so your javascript breaks and doesn't work.

You can add class or some attribute to the element that has already has Fancybox init so you don't initialized it again for the same element and your javascript beaks as below :

$('a.fancy-img').live('click', function () {
            var El = $(this);
            $(this).fancybox({});
            if (!El.hasClass('fancy-box-init')) {
                El.addClass('fancy-box-init');
                El.trigger('click');
            }
            return false;
 });
Meabed
  • 3,828
  • 1
  • 27
  • 37
  • `.live()` is deprecated, use `.on()` (jQuery v1.7+) instead – JFK Apr 04 '14 at 19:04
  • @JFK yes but from his code and other comment he is using older version of jQuery. so .live works fine. thanks for the comment. – Meabed Apr 04 '14 at 21:24