-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!

4
  • You don't seem to ever call your function. If you want a different image upon load, call your function when the page loads. Commented Oct 11, 2022 at 13:57
  • stackoverflow.com/a/7312588/14733216 Commented Oct 11, 2022 at 13:57
  • Where should I call my the function from the script? Commented Oct 11, 2022 at 13:58
  • where do you call randomImage? Commented Oct 11, 2022 at 14:11

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

Does not really answer the question.
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.
Hello my friend I am not a professional who can help, but I would like to help as much as I can.
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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.