Javascript DOM HTML Image src Property display random image
<!DOCTYPE html> <html lang="en"> <body> <img src="image1.png" onclick="changeImg(this)" /> <img src="image2.png" onclick="changeImg(this)" /> <script> let myImages = [ //from w w w. j a va 2s . c o m "image1.png", "image3.png", "image4.png", "image2.png" ]; function changeImg(that) { let newImgNumber = Math.round(Math.random() * 3); while (that.src.indexOf(myImages[newImgNumber]) != -1) { newImgNumber = Math.round(Math.random() * 3); } that.src = myImages[newImgNumber]; } </script> </body> </html>