Cache jQuery objects
Look out for
Making the same jQuery selections within functions and across code blocks.
Resolve by
Storing jQuery objects as variables so they can be reused.
$('.photo-list').on( 'click', 'a', function( event ) {
// make jQuery selections with each click
$('.gallery__image').attr( 'src', $( this ).attr('href') );
$('.gallery__title').text( $( this ).text() );
$('.gallery__caption').text( $( this ).attr('title') );
});
// get jQuery objects once
var $galleryImage = $('.gallery__image');
var $galleryTitle = $('.gallery__title');
var $galleryCaption = $('.gallery__caption');
$('.photo-list').on( 'click', 'a', function( event ) {
// get clicked element
var $link = $( this );
// use cached jQuery objects
$galleryImage.attr( 'src', $link.attr('href') );
$galleryTitle.text( $link.text() );
$galleryCaption.text( $link.attr('title') );
});