The script below applies random colours from an array to a div id, and changes the colours on hover. I would like to amend it so that it loops through a div class and applies random colours to all classes on a page. Obviously the click functions would have to be done by id, but I imagine there is a way of looping through the div classes such that they all have different random colours which change on hover.
Could someone help with this?
Thanks
Nick
$(document).ready(function() {
var test = $("#example").text().split('');
var normal = generateColors(test);
var hover = generateColors(test);
$("#example").html(normal);
$("#example").hover(
function(event) { $("#example").html(hover) },
function(event) { $("#example").html(normal) });
$("#example").click(function() {
location.href = "http://www.google.co.uk";
});
});
function generateColors(characters) {
var result = "";
var i = 0;
for(i=0; i < characters.length; i++) {
result += "<span style='color:"+getColor()+"'>"+characters[i]+"</span>";
}
return result;
}
function getColor() {
var colList = ['#7EA404', '#14AFB0','#B05718', '#B0A914', '#B01617','#902BB0', '#B003A2', '#4A429C','#33821E', '#226795', '#D0B600','#886833'];
var i = Math.floor((Math.random()*colList.length));
return colList[i];
}
To apply the colors, and store them on the different elements, you could do:
Setting each link with location.href on click seems like a really bad idea, but up to you.