nienn6 nienn6 - 1 year ago 55
Javascript Question

Random image click

So I got a site full of images and they are are all in random order, everytime you refresh the page. You have to search for a image which is always on a different place.

function randomWaldo() {
var randomNummer = Math.floor((Math.random() * 100) + 1);
document.getElementsByTagName('img')[randomNummer].src = "images/waldo.jpg";}

Now I want the first paragraph in my HTML to change when you click on that one image. How can I do this?

Answer Source

You can try to select the image who have the wanted source.

And add a class or id to your paragraph for readability.

document.getElementById("paragraphid").innerHTML="You win";


  document.getElementById("rep").innerHTML="you win !";
<p id="rep"></p>
<img src="image/nok.jpg" alt="nok">
<img src="image/nok.jpg" alt="nok">
<img src="image/nok.jpg" alt="nok">
<img src="image/waldo.jpg" alt="waldo">
<img src="image/nok.jpg" alt="nok">