fancybox icon indicating copy to clipboard operation
fancybox copied to clipboard

fancybox does not work when selector starts not from root

Open yarosla opened this issue 14 years ago • 5 comments

Excerpt from fancybox source code:

    $.fn.fancybox = function (options) {
        var selector = this.selector || '';
        ...
          if (selector) {
            D.undelegate(selector, 'click.fb-start').delegate(selector, 'click.fb-start', run);
          } else {
            that.unbind('click.fb-start').bind('click.fb-start', run);
          }

There are scenarios when selector is non-empty but can not be used to select nodes starting from $(document). For example:

$(document.getElementById('some-id')).children('.ch1').fancybox();

In this case selector === '.children(.ch1)' and evaluating it from $(document).find(selector) gives empty result so fancybox does not function.

This scenario is quite typical when trying to invoke fancybox from whithin other jquery plugin.

yarosla avatar Mar 04 '12 01:03 yarosla

Here is a StackOverflow question in which someone trips over this issue: http://goo.gl/88R4c

In my opinion, it's a design problem for a plugin to rely on there being a usable selector on the jQuery object. The library does not ensure that non-empty selector values are actually usable.

Pointy avatar Mar 19 '12 14:03 Pointy

I just ran into this problem today. I'm calling Fancybox on a complex selector, and it is not able to resolve the url of the link that I'm clicking on.

timdmackey avatar Apr 02 '12 18:04 timdmackey

I just ran into this too when I did this:

var count = 0;
$('.photoGallery').each(function () {
    count++;
    var $links = $(this).find('a');
    $links.attr('data-fancybox-group', 'group'+count);
    $links.fancybox({
        padding: 0
    });
});

because the selector gets set as "a", which then screws up every link on the page. This seems like it should be a perfectly acceptable use case. I bypassed the problem for myself below, but this should probably be fixed:

var count = 0;
$('.photoGallery').each(function () {
    count++;
    var $links = $(this).find('a');
    $links.attr('data-fancybox-group', 'group'+count);
});
$('.photoGallery a').fancybox({
    padding: 0
});

AdamPflug avatar Apr 11 '12 18:04 AdamPflug

You can use $($links.get()) to remove selector :

$('.photoGallery').each(function (key) {
    var $links = $(this).find('a');
    $links.attr('data-fancybox-group', 'group'+key);
    $($links.get()).fancybox({
        padding: 0
    });
});

But a real fix would be better... (FancyBox2 use jQuery.selector and jQuery.live() that are deprecated)

yukulele avatar Jul 27 '12 14:07 yukulele

Sorry, I forgot to comment that you can turn off the "live" feature, e.g., if you have complex selectors like -

$("#something").children().filter('a').fancybox();

than this might not work as expected and this how you can fix -

$("#something").children().filter('a').fancybox({
    live : false
});

fancyapps avatar Aug 31 '12 08:08 fancyapps