$(document).ready(function(){ $('.sjimg li img').on('click',function(){ var src = $(this).attr('alt'); var img = ''; //start of new code new code var index = $(this).parent('li').index(); var html = ''; html += img; html += '
'; //html += ''; //html += ''; html += '
'; $('#mymodal').modal(); $('#mymodal').on('shown.bs.modal', function(){ $('#mymodal .modal-body').html(html); //new code $('a.controls').trigger('click'); }) $('#mymodal').on('hidden.bs.modal', function(){ $('#mymodal .modal-body').html(''); }); }); $('.sjivideo li img').on('click',function(){ var src = $(this).attr('alt'); var img = ''; //var img = ''; //start of new code new code var index = $(this).parent('li').index(); var html = ''; html += img; html += '
'; //html += ''; //html += ''; html += '
'; $('#mymodal').modal(); $('#mymodal').on('shown.bs.modal', function(){ $('#mymodal .modal-body').html(html); //new code $('a.controls').trigger('click'); }) $('#mymodal').on('hidden.bs.modal', function(){ $('#mymodal .modal-body').html(''); }); }); }) $(document).on('click', 'a.controls', function(){ var index = $(this).attr('href'); //var src = $('ul.sjimg li:nth-child('+ index +') img').attr('alt'); var src = $(this).parent('li:nth-child('+ index +') img').attr('alt'); $('.modal-body img').attr('src', src); var newprevindex = parseint(index) - 1; var newnextindex = parseint(newprevindex) + 2; if($(this).hasclass('previous')){ $(this).attr('href', newprevindex); $('a.next').attr('href', newnextindex); }else{ $(this).attr('href', newnextindex); $('a.previous').attr('href', newprevindex); } var total = $('ul.sjimg li').length + 1; //hide next button if(total === newnextindex){ $('a.next').hide(); }else{ $('a.next').show() } //hide previous button if(newprevindex === 0){ $('a.previous').hide(); }else{ $('a.previous').show() } return false; });