-3

In my html website I have a random number function:

   <script>
    var randomnumber=Math.floor(Math.random()*11)
    document.getElementById("random").innerHTML = randomnumber;
   </script>

I would like to add a "reload" button that changes the random number generated, without reloading the page. Is there any way to do this besides the location reload?

D Farmer
  • 3
  • 3
  • 2
    Put the code inside a function and bind that function to the button click event – andrew May 20 '16 at 16:05
  • 1
    Possible duplicate of [Trigger a button click with JavaScript on the Enter key in a text box](http://stackoverflow.com/questions/155188/trigger-a-button-click-with-javascript-on-the-enter-key-in-a-text-box) – Alon Eitan May 20 '16 at 16:09

1 Answers1

1

Whenever you are going to repeat a process, you should use functions. Your calculation is right. You just need to put it inside a function and call it in case of clicking the button.

  function changeRandom(){
    var randomnumber=Math.floor(Math.random()*11)
    document.getElementById("random").innerHTML = randomnumber;
  }
changeRandom();
<div id="random">
</div>
<button onclick='changeRandom()'>Click</button>
Mojtaba
  • 4,852
  • 5
  • 21
  • 38