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.
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.
<div id="myDiv"> <a href="http://www.google.com">Google</a> <a href="http://www.yahoo.com">Yahoo</a> <a href="http://www.live.com">Live</a> <a href="http://www.google.com">Google</a> <a href="http://www.yahoo.com">Yahoo</a> <a href="http://www.live.com">Live</a> <a href="http://www.google.com">Google</a> <a href="http://www.yahoo.com">Yahoo</a> <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):
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'.