Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generate Random Numbers for Several Text Boxes 1

Status
Not open for further replies.

DebbieC

Programmer
Mar 29, 2001
168
US
I have a form that has 5 text boxes that each need to be filled with 4 random numbers when one button is pressed to generate them.

I don't know Javascript that well so I researched on the internet and found some code to generate random numbers. The code looks like it should work (from my limited knowledge). It returns an error message that says runtime error on the line that says "document.getElementById('ranlist1').innerHTML=rannum".

Here is the code:

Code:
<script type="text/javascript">
function genNumbers() {
  var i, rannum;

  for(i = 0; i < 100; i++) {

    rannum = Math.random()*4;

    rannum = Math.round(rannum);

	document.getElementById('ranlist1').innerHTML=rannum 
	document.getElementById('ranlist2').innerHTML=rannum 
	document.getElementById('ranlist3').innerHTML=rannum 
	document.getElementById('ranlist4').innerHTML=rannum 
	document.getElementById('ranlist5').innerHTML=rannum 

  }
}

</script>


<input type="text" name="ranlist1" size="5" value=""> - 
<input type="text" name="ranlist2" size="5" value=""> -
<input type="text" name="ranlist3" size="5" value=""> -
<input type="text" name="ranlist4" size="5" value=""> -
<input type="text" name="ranlist5" size="5" value="">


<input type="button" value="Generate Code" id="Generate" onclick="genNumbers()";>

I just realized that rannum will run once and return the same value to all of the fields the way I have it now. However I need the values in all of the fields to be randomly generated.

Does anyone know a better way to do this?


 
Code:
<script type="text/javascript">
function genNumbers() {
   document.getElementById('ranlist1').[b][COLOR=red]value[/color][/b]=rannum[b][COLOR=red]()[/color][/b]
   document.getElementById('ranlist2').[b][COLOR=red]value[/color][/b]=rannum[b][COLOR=red]()[/color][/b]
   document.getElementById('ranlist3').[b][COLOR=red]value[/color][/b]=rannum[b][COLOR=red]()[/color][/b]
   document.getElementById('ranlist4').[b][COLOR=red]value[/color][/b]=rannum[b][COLOR=red]()[/color][/b]
   document.getElementById('ranlist5').[b][COLOR=red]value[/color][/b]=rannum[b][COLOR=red]()[/color][/b]
}
[b][COLOR=red]
function rannum() {
   return (Math.round(Math.random()*4));
}
[/color][/b]
</script>


<input type="text" [b][COLOR=red]id[/color][/b]="ranlist1" size="5" value=""> -
<input type="text" [b][COLOR=red]id[/color][/b]="ranlist2" size="5" value=""> -
<input type="text" [b][COLOR=red]id[/color][/b]="ranlist3" size="5" value=""> -
<input type="text" [b][COLOR=red]id[/color][/b]="ranlist4" size="5" value=""> -
<input type="text" [b][COLOR=red]id[/color][/b]="ranlist5" size="5" value="">


<input type="button" value="Generate Code" id="Generate" onclick="genNumbers()";>

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
I tried it and it works great!!!! Thank you sooooooo much!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top