Log in

View Full Version : HTML: On button click, show number from list.?



elliott123
09-08-2011, 10:49 PM
Hello
This is what I'd like to do:

Click on a button, then a 4 digit number appears.
This 4 digit number will be one of a five hundred, say, that have already been generated.

So basically, when you click the button, it picks a random number from the number list.
Is there a HTML code for this (or a code that will work in iWeb)

If that makes sense, just say if i haven't explained properly.

Thankyou!

andrewjm222
09-08-2011, 11:02 PM
no it seems what you looking for is a random number script you would have to use javascript or another scripting language

elliott123
09-08-2011, 11:06 PM
could I use PHP?

How would I do it with code (php or javascript)?

andrewjm222
09-08-2011, 11:10 PM
ill post it for javascript below:

<html>
<head></head>
<body>
<h2>The random number produced was
<script type="text/javascript">
document.write(
Math.floor(Math.random()* 11)
);
</script>
</body>

the 11 means the random number will be between 0 and 11

elliott123
09-08-2011, 11:14 PM
Okay thankyou, but could I have a 4 digit number that's selected from a predefined list (that i've made), instead of just a random number between x and y?

andrewjm222
09-08-2011, 11:16 PM
hmm give me an example and ill tweek it for ya

elliott123
09-08-2011, 11:19 PM
So, I have five 4-digit numbers, this is my pre defined list.
1982
5982
7391
9263
3681

I want a 4 digit number that's selected from my predefined list.
So, the script randomly picks one of the above numbers (in the list), but not any old 4 digit number.
E.g. It could pick 1982 (because it's in the list) but not 1234 (because it's not in the list)

That any help? :P

andrewjm222
09-08-2011, 11:21 PM
ok give me like 15 mins to create it for ya

elliott123
09-08-2011, 11:26 PM
cheers buddy!

andrewjm222
09-08-2011, 11:31 PM
here it is:

<html>
<head></head>
<body>
<h2>The random number produced was
<script type="text/javascript">

var random = Math.floor(Math.random()* 4)

switch(random)
{
case 0:
alert("1982");
break;
case 1:
alert("5982")
break;
case 2:
alert("7391");
break;
case 3:
alert("9263")
break;
case 4:
alert("3681")
break;
</script>
</body>

This will set an alert once a page is loaded, the alert will have the 4 digit code in it:dance:

elliott123
09-08-2011, 11:42 PM
ahh it doesn't work :(
nothing happens when the page loads (other then the text 'the random number prdouced was')
there's no number??

Want to hide these adverts? Register an account for free!