1

I am trying to make every letter in a small line of text a different random color. I can only seem to make it do this when I use .hover, but I want it to do the action straight away (once the page loads). Please help. I would also like to know if anyone knows how to just set different colors on each letter (set colors) with css or Javascript because when I tried to do it in that way, it wouldnt allow me call a function on the same div/id (i tried to arch the text). Thank you.

THIS IS MY CODE

var colours = ["#000000", "#FF0000", "#990066", "#FF9966", "#996666", "#00FF00", "#CC9933"], 
    idx;

$("#arch").hover(function(){
    var div = $(this); 
    var chars = div.text().split('');
    div.html('');     
    for(var i=0; i<chars.length; i++) {
        idx = Math.floor(Math.random() * colours.length);
        var span = $('<span>' + chars[i] + '</span>').css("color", colours[idx])
        div.append(span);
    }

}, function() {
    $(this).find('span').css("color","#FF0000");
});

2 Answers 2

2
    var colours = ["#000000", "#FF0000", "#990066", "#FF9966", "#996666", "#00FF00", "#CC9933"], 
    idx;

$(function() {
    var div = $('#arch'); 
    var chars = div.text().split('');
    div.html('');     
    for(var i=0; i<chars.length; i++) {
        idx = Math.floor(Math.random() * colours.length);
        var span = $('<span>' + chars[i] + '</span>').css("color", colours[idx]);
        div.append(span);
    }
});

Works for me. jsFiddle

You have to make sure the DOM is loaded before doing any jQuery stuff on it.

http://learn.jquery.com/using-jquery-core/document-ready/

Sign up to request clarification or add additional context in comments.

Comments

1

Try (untested):

$(document).ready(function(){
    var div = $('#arch'); 
    var chars = div.text().split('');
    div.html('');     
    for(var i=0; i<chars.length; i++) {
        idx = Math.floor(Math.random() * colours.length);
        var span = $('<span>' + chars[i] + '</span>').css("color", colours[idx])
        div.append(span);
    }
}

Basically, you need to wait for the DOM to load so that there's a div to find when you call $('#arch')!

I'm not sure what you mean by "tried to arch the text".

1 Comment

I am trying to arch/curve the text, but when I try to do this with the random color thing, it does not arch the text. CODE -> $("#arch").arctext({radius: 300}) var colours = ["#000000", "#FF0000", "#990066", "#FF9966", "#996666", "#00FF00", "#CC9933"], idx; $(function() { var div = $('#arch'); var chars = div.text().split(''); div.html(''); for(var i=0; i<chars.length; i++) { idx = Math.floor(Math.random() * colours.length); var span = $('<span>' + chars[i] + '</span>').css("color", colours[idx]); div.append(span); } });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.