I\'m trying to alternate the colors of every letter in a specific div on my webpage using javascript.
I found this script on a forum that alternates the color of the wor
I've got a simple demo:
This assumes that the text is already on the page, only affects text (using innerText), and replaces the text inside the div with the required html
function alternateColours() {
var div = document.getElementById("alternator");
var message = div.innerText;
html = "";
var colors = new Array("#ff0000","#00ff00","#0000ff"); // red, green, blue
for (var i = 0; i < message.length; i++)
html += "" + message[i] + " ";
div.innerHTML = html;
}
alternateColours();
I would recommend the use of jQuery to attach the function to the page load, much cleaner that having to do it yourself.