...

View Full Version : Problems applying random function to a div



jwevver01
06-20-2009, 12:59 PM
Hi,

I have found the following code posted by fellow user called Thalantyr in 2006, which randomly changes the colour of buttons and images. It basic but works fine as is.

I would like to apply the random function to change the background image of a <div>. The div already has an id with css properties attached. Is there away to apply the function using a class? I have tried changing the 'var eles=document.getElementsByTagName('*')||document.all;' section but it stops working. Code below.

Any suggestions or comments are welcome.

Many thanks in advanced.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
var ColAry=['red','green','blue','yellow']
var ImgAry=['One.gif','Two.gif','Three.gif','Four.gif']

function RandomColor(){
ColAry=ColAry.randomise();
var eles=document.getElementsByTagName('*')||document.all;
for (var zxc0=0;zxc0<eles.length;zxc0++){
if (eles[zxc0].className=='Random'){
eles[zxc0].style.backgroundColor=ColAry[0];
}
}
}

function RandomImg(){
ImgAry=ImgAry.randomise();
var eles=document.getElementsByTagName('*')||document.all;
for (var zxc0=0;zxc0<eles.length;zxc0++){
if (eles[zxc0].className=='RandomImg'){
eles[zxc0].src='http://www.vicsjavascripts.org.uk/StdImages/'+ImgAry[0];
}
}
}

Array.prototype.randomise=function(){
zxccnt=0; zxcta=[];
while (zxccnt<this.length){
zxctmp=Math.floor(Math.random()*this.length-1)+1;
if (!zxcta[zxctmp]){zxcta[zxctmp]=this[zxccnt]; zxccnt++; }
}
return zxcta;
}

//-->
</script></head>

<body onload="RandomColor();RandomImg();" >
<input class="Random" type="button" name="" value="Button 1">
<input class="Random" type="button" name="" value="Button 2">
<input class="Random" type="button" name="" value="Button 3">
<input type="button" name="" value="Button">
<input class="Random" type="button" name="" value="Button 4">
<input class="Random" type="button" name="" value="Button 5">
<input class="Random" type="button" name="" value="Button 6">
<input class="Random" type="button" name="" value="Button 6">
<img class="RandomImg"href="Blank.gif" width=50 height50 >
<img class="RandomImg"href="Blank.gif" width=50 height50 >
<img class="RandomImg"href="Blank.gif" width=50 height50 >
<img class="RandomImg"href="Blank.gif" width=50 height50 >
<img class="RandomImg"href="Blank.gif" width=50 height50 >
<img class="RandomImg"href="Blank.gif" width=50 height50 >
<img class="RandomImg"href="Blank.gif" width=50 height50 >
<img class="RandomImg"href="Blank.gif" width=50 height50 >
<img class="RandomImg"href="Blank.gif" width=50 height50 >
<img class="RandomImg"href="Blank.gif" width=50 height50 >

</a></body>

</html>

stick_branch
06-20-2009, 04:22 PM
var eles = document.getElementsByTagName('div');
for(var count = 0;count<=eles.length;count++){
if(eles[count].class=="random")eles[count].style.background = randomColour;
}

randomColour must be in hex.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum