This variable
bildlink = $('div.bildlink').html();
... will only get the first element with class bildlink
so this is why. You need to target each bildlink
selector according to the index
of the clicked fancybox selector using the .eq()
method like :
jQuery(document).ready(function ($) {
$(".album").fancybox({
cyclic: 'false',
afterLoad: function () {
var bildlink = $('div.bildlink').eq(this.index).html();
this.title = this.title + ' <a target="_blank" href="' + bildlink + '">' + bildlink + '</a> ';
},
helpers: {
title: {
type: 'inside'
}
},
}); // fancybox
}); // ready
Notice that you still need to re-initialize the variable bildlink
every time within the callback afterLoad
, to set the proper index
.
See forked JSFIDDLE
NOTE : this solution assumes that all .bildlink
selectors follow the same consecutive order in the DOM as the .album
selectors