I’m trying to get each letter in a div element to change to a random colour from an array of colours. Then reset when the mouse moves off the div.
Here’s what I’ve got so far. I think I’m pretty close, apart from the fact it doesn’t actually work. This was built from a few different snippets on this site.
$(document).ready(function() {
// COLOURS ARRAY
var colours = Array("#ddd", "#333", "#999", "#bbb"), idx;
$("DIV#header").hover(function(){
$( $(this).text().split('')).each(function(index, character) {
idx = Math.floor(Math.random() * colours.length);
$(this).css("color", colours[idx]);
});
}, function() {
$(this).css("color","#ddd");
});
});
It doesn’t produce any JS errors. The 2nd function of the hover seems to work but not the first. Any help would be gratefully received!
A string is not an element and you cannot add a CSS property to it. You can put your letters in span elements and then style them, try this:
http://jsfiddle.net/BC5jt/