0
votes

Having difficulty getting fancybox options to show up only when my lightbox link has an id class in it. Here's what I have so far (which breaks it, and just goes straight to the image in a new page):

<script type="text/javascript">
    var thisHash = window.location.hash;
    $(document).ready(function() {
        $(".fancybox").fancybox({
            helpers : { 
                title : { type : 'inside' }
            }, // helpers
            beforeShow : function() {  //Where my issue starts
                if(typeof(this.element.attr("id")) !== 'undefined'){ 
                    this.title = (window.location + '#' + this.element.attr("id"));  
            }, // beforeShow
        }); // fancybox
        if (thisHash) {
            $(thisHash).trigger('click');
        }
    }); // ready
</script>

Any suggestions? I thought filtering for "undefined" would do it, since when the id class is missing it normally returns "undefined" when I echo it, but maybe I'm just not using correct syntax for this since this is javascript and I've been dealing with PHP mostly.

Anyone? I know the fancybox group is pretty sparse, but was hoping someone might be out there trolling it. - dotcommer
Could you show how your html looks like? Also, could you be more specific about what you mean with an id class? (maybe you meant an id attribute) - JFK
Otherwise your code works fine for me jsfiddle.net/ytybcoc9 - JFK
A typical link on my site would look like this: <a id="example1" class="fancybox" data-fancybox-group="example" href="path/to/image.jpg" title="image title">Image</a> But in one section, I don't have links generated with an id, so i'd like to omit some details I add to a fancybox window if it doesn't detect an id tag. Interesting your fiddle works and its the same code, yet mine isn't working for me. - dotcommer
Most likely you have other js error that is voiding fancybox from working - JFK