Click to See Complete Forum and Search --> : Could this be done and how???????????


sciguyryan
08-10-2003, 11:32 AM
hi, all


is this possible?


could you write a code to get the date and time to generata a rondom number. then, you have an array with multiple colours init and a button that OnClick uses the date and time function to get the number and then that number is matched with the one on the array and, it gives a colour that is nearly random???????????
can this be done and if so how???????????
if you could provide a code i would be most greatfull:)

AdamGundry
08-10-2003, 01:00 PM
You don't need to explicitly use JS date/time functions to generate the random number, as in Javascript the random number generator is automatically seeded by the date/time. You should be able to do something like this:

<button onclick="chooseColour()">Choose Colour</button>
<script type="text/javascript">
function chooseColour(){
var colsArray = ["red", "blue", "yellow"];
colVar = colsArray[parseInt(Math.random()*colsArray.length)];
alert(colVar);
}
</script>

Adam

sciguyryan
08-11-2003, 03:59 AM
hi,



how could i make the documents colour the selected colour???????????????

AdamGundry
08-11-2003, 05:33 AM
I believe you can do this instead of the alert:

document.body.style.backgroundColor = colVar;

Adam

sciguyryan
08-11-2003, 05:40 AM
hi (again),


thanks that works a treat.