eainnem eainnem - 5 months ago 8x
HTML Question

Creating a random number using JavaScript and returning in HTML without using form

Is it possible to generate a random number from JavaScript to HTML using button

but return it through a
? I have looked at other similar questions but they all seem to use either
. I am not getting any errors in my browser, when I try this code but the output is coming from the
saying 'undefined' as opposed to a number so I'm obviously not defining
but I'm just a bit confused by JavaScripts syntax.

/// HTML ///

<script type="text/javascript" src="randRandomScript.js">


<button name="randNumberButton" onclick="randNumberScript()" >Click for Number</button>

<div id="number"></div>


/// JavaScript ///

var randomNumberObject = math.floor(math.random()*10);

function randNumberScript () {

document.getElementById("number").innerHTML = randomNumberObject;



Use randomNumberObject variable as local-variable or else it will not be updated every time button clicked.

Note: Correct the typo @ math.. It is Math

function randNumberScript() { //Why are you accepting a argument ?
  var randomNumberObject = Math.floor(Math.random() * 10); //typo at Math
  document.getElementById("number").innerHTML = randomNumberObject;
<button name="randNumberButton" onclick="randNumberScript()">Click for Number</button>

<div id="number"></div>