var exposure = 20; var input_index = 0; function setpic() { $("#pic").attr("src", exposure + "/" + files[input_index]); } $(function() { setpic(); }); Mousetrap.bind('z', function() { $("#pic").toggleClass("fit_view"); }); Mousetrap.bind('n', function() { input_index ++; setpic(); }); Mousetrap.bind('p', function() { input_index --; setpic(); });