Click to See Complete Forum and Search --> : Changing random number script to a form input box


jake_604
12-05-2002, 01:53 PM
Hi, how can I change this random number script so that is displays the random number in an input field on a form?


<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
document.write(getRandomNum(10000,99999));
// End -->
</SCRIPT>

:confused: :confused: :confused:

AdamGundry
12-05-2002, 02:32 PM
<SCRIPT LANGUAGE="JavaScript" type="text/javascript"><!--
document.formname.fieldname.value = getRandomNum(10000,99999);
// End -->
</SCRIPT>


Try this

Adam

jake_604
12-05-2002, 02:40 PM
are you sure this works?

AdamGundry
12-05-2002, 02:48 PM
This is the usual way to refer to an input field using JavaScript, and works in most (if not all) browsers. It is possible the function you are calling is somehow incompatible - posting the code would help.

Alternately, check the form and input both have appropriate NAME attributes.

Adam

jake_604
12-05-2002, 02:54 PM
IN THE HEADER:


<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
function getRandomNum(lbound, ubound) {
return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}
// End -->

</SCRIPT>



IN THE BODY:


<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
document.write(getRandomNum(10000,99999));
// End -->
</SCRIPT>

jake_604
12-06-2002, 12:28 PM
please can sum1 help me!!!!!!

gil davis
12-06-2002, 03:59 PM
I guess you want the whole answer, because you got all the pieces you needed.

<html>
<head>
<script>
function getRandomNum(lbound, ubound) {
return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}
function init() {
document.f1.i1.value = getRandomNum(10000,99999));
}
</script>
<body onLoad="init()">
<form name="f1" onSubmit="return false">
<input type="text" name="i1" size="20">
</form>
</body>
</html>

jake_604
12-07-2002, 06:19 AM
aaaaaaawwwwwwwww, i cant get that previous script to work... :( :( :( :(

gil davis
12-07-2002, 08:08 AM
function init() {
document.f1.i1.value = getRandomNum(10000,99999));
}
Should be:

function init() {
document.f1.i1.value = getRandomNum(10000,99999);
}

One too many parentheses.

jake_604
12-07-2002, 10:10 AM
that works, thanks all for your help :D