Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problems applying random function to a div

    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>

  • #2
    New Coder
    Join Date
    Oct 2008
    Location
    Australia
    Posts
    32
    Thanks
    1
    Thanked 1 Time in 1 Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •