function randomImg(n, prefix) { //Generate a random number from 1 to n var ranNum = Math.floor(Math.random() * n) + 1; //Create the random id by concatenating the prefix and the random number var id = prefix + ranNum; if (document.getElementById(id)) { document.getElementById(id).style.display = 'inline'; } }