Setting Text Size and Color

Discussion in 'JavaScript' started by phatuis, Oct 2, 2009.

  1. #1
    Hi, I want to be able to use math.random to set the size and color of text, to random, and the colors must be set, like yellow, red and orange, how could I do this? I want to do this using the a href tags in html, to make each link a different size and different color. Thanks.
     
    phatuis, Oct 2, 2009 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    how good are you with javascript?
    1. create an array that contains all the colours that you want.
    2. create a div that contains the links, lets say that it is assigned to 'myDiv'.
    3. use myDiv.getElementsByTagName("a") to get all the links from within the div.
    4. iterate through these and get a different random number each time to get a random colour from the array.
    5. get a random number from say 10 - 18 for the font size.
    6. assign these to the style of each link.
     
    camjohnson95, Oct 2, 2009 IP
  3. phatuis

    phatuis Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    How do I assign them to each link?
     
    phatuis, Oct 2, 2009 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    
            <div id="myDiv">
            <a href="http://www.google.com">Google</a>&nbsp;<a href="http://www.yahoo.com">Yahoo</a>&nbsp;<a href="http://www.live.com">Live</a>
            <a href="http://www.google.com">Google</a>&nbsp;<a href="http://www.yahoo.com">Yahoo</a>&nbsp;<a href="http://www.live.com">Live</a>
            <a href="http://www.google.com">Google</a>&nbsp;<a href="http://www.yahoo.com">Yahoo</a>&nbsp;<a href="http://www.live.com">Live</a>  
            </div>
            <script type="text/javascript">
            window.onload = function() {
                var whatColor, whatSize;
                var myColors = ["Red","Green","Blue","Pink","Yellow","Purple","Orange"];
                var fontMin = 10;
                var fontMax = 20;
                var div1 = document.getElementById("myDiv");
                var links = div1.getElementsByTagName("a")
                for(i=0;i<links.length;i++) {
                    whatColor = Math.floor(Math.random() * myColors.length);
                    whatSize = Math.floor(Math.random() * (fontMax - fontMin + 1)) + fontMin;
                    links[i].style.color = myColors[whatColor];
                    links[i].style.fontSize = whatSize + "px";
                }
            }
            </script>
    
    Code (markup):
     
    camjohnson95, Oct 2, 2009 IP
  5. phatuis

    phatuis Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It didnt work, the links still display as normal?
     
    phatuis, Oct 2, 2009 IP
  6. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    The script works fine in both IE and FF
    1. Are all the links placed within a DIV that has an ID of myDiv ?
    2. What error are you receiving ?
    3. What Browser are you using ?
    4. If you are running it locally within IE, you may have to click the yellow bar at the top and 'Allow Blocked Content'.
     
    camjohnson95, Oct 2, 2009 IP
  7. phatuis

    phatuis Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I am running on Safari, it is now working, thanks for your help mate.
     
    phatuis, Oct 2, 2009 IP