This code ensures that when an image is clicked on a page of portfolio images to go to the corresponding gallery, the image that was clicked on shows up as the first image. It does this by comparing chars from the src attributes and using .prependTo. It all works fine when I navigate back and forth between the portfolio and gallery pages using the links on the pages, but it all gets wonky when I use the back button on the browser, sometimes prepending over and over. Is it something to do with the click event firing over and over? I'm not sure...
$(document).on('click',"a.project",function(){
$("a.project").on('click', function(event) {
var portfolioLastTwoChars = $(event.target).attr('data-src').slice(-8, -4);
var galleryLastTwoChars = $("img").first().attr('data-src').slice(-8, -4);
if (portfolioLastTwoChars === galleryLastTwoChars) {
return;
} else {
var prePendedImage = $("div").children('img[alt*=' + portfolioLastTwoChars + ']').not(".loaded");
$(prePendedImage).not(':first').remove();
prePendedImage.prependTo("div.image-list");
$('div.image-list > img:not(:first-child)').remove();
}
});
});
$( document ).ready(function() {
$("a.project").eq(0).trigger('click');
});