Ofcdan Ofcdan - 3 months ago 13
HTML Question

Random number generator w/ button

Basically I have this.. but I want the BUTTON to generate the random number.. which currently only happens when I run the html file, which I don't want that to happen. Thanks!

<body>
<form name="rn">
<input type="text" id="tb" name="tb" /></br>


<input type="button" value="Random Number!" onclick="doFunction(Random);" />
</form>



<script type="text/javascript">
function Random() {
return Math.floor(Math.random() * 1000000000);
}




document.getElementById('tb').value = Random()
</script>



Answer

Try changing your HTML to this

<form name="rn">
    <input type="text" id="tb" name="tb" />
    <input type="button" value="Random Number!" onclick="Random();" />
</form>

and your javascript to this

function Random() {
        var rnd = Math.floor(Math.random() * 1000000000);
        document.getElementById('tb').value = rnd;
    }

Working fiddle