Javascript DOM Click event via HTML Tag onclick event handler
<!DOCTYPE html> <html lang="en"> <body> <img id="img0" src="image1.png" /> <img id="img1" src="image2.png" /> <script> let myImages = [ /*w ww.j ava 2 s.co m*/ "image1.png", "image3.png", "image4.png", "image2.png" ]; function changeImg(e) { let el = e.target; let newImgNumber = Math.round(Math.random() * 3); while (el.src.indexOf(myImages[newImgNumber]) != -1) { newImgNumber = Math.round(Math.random() * 3); } el.src = myImages[newImgNumber]; } document.getElementById("img0").onclick = changeImg; document.getElementById("img1").onclick = changeImg; </script> </body> </html>