Just wondering why my code isn’t working. Am I doing this completely wrong?
window.onload = function random() {
var links = document.getElementsByTagName('a');
var colours = new Array("green","red","blue");
var randomColour = colours[Math.floor(colours.length * Math.random())];
for (a = 0; a < links.length; a++) {
links[a].style.color = 'randomColour';
}
}
'randomColor'->randomColor.Use a variable instead of a quoted string. Also, to ensure that each link gets a random color, generate the color in the loop:
To minimize the delay between setting the color, and parsing the link, bind a
DOMContentLoadedevent, or simply paste the code at the end of the<body>:http://jsfiddle.net/V6Chb/1/