-2
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Dicee</title>
    <link rel="stylesheet" href="styles.css">
    <link href="https://fonts.googleapis.com/css?family=Indie+Flower|Lobster" rel="stylesheet">

  </head>
  <body>

    <script src="index.js"></script>
    <div class="container">
      <h1>Refresh Me</h1>

      <div class="dice">
        <p>Player 1</p>
        <img class="img1" src="images/dice6.png">
      </div>

      <div class="dice">
        <p>Player 2</p>
        <img class="img2" src="images/dice6.png">
      </div>

    </div>



  </body>

  <footer>
      Tudor R. Ardelean © 2022  
  </footer>
</html>

function randomImage()
{
  document.querySelector(".img1").setAttribute("src", "images/dice" + (Math.floor(Math.random() * 6) + 1) + ".png");
}

How do I use the script that I wrote (just that function) to replace the image with another one? I have the other 5 dice pics saved in my computer. So every time when I refresh the page, I should get another one, another one, another one, and so on. Thanks in advance!

1 Answers1

0

can you like this

<!DOCTYPE html>

<html>

<body>

<button onclick="document.getElementById('myImage').src='pic_bulbon.gif'">Turn on the light</button>

<img id="myImage" src="pic_bulboff.gif" style="width:100px">

<button onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn off the light</button>

</body>

</html>
Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
codeLover
  • 16
  • 3
  • Does not really answer the question. – epascarello Oct 11 '22 at 14:12
  • I actually opened the developer tools on my browser, pasted only the line of code from my function, and it works, but the problem is that I want that function to run every time I refresh the website, so not having to copy and paste over and over the line in the console. My question is where should I call the function in my html code page? I only linked the script at the bottom of the div where I have the pics for those two dices. – businessuser_01 Oct 11 '22 at 14:17
  • Hello my friend I am not a professional who can help, but I would like to help as much as I can. – codeLover Oct 11 '22 at 14:48
  • Guys I figured it out it was only a matter of simply writing the code in the .js file and linking it properly at the end of the .html document. But thanks for your time anyway! – businessuser_01 Oct 20 '22 at 14:54