3

I am trying to create a function that will change the color of a span from black to a random color from a list of colors I have defined. My problem seems to be in the .css("color", variableName) part. I think I may be doing my syntax wrong. ( http://jsfiddle.net/crismanNoble/8gM76/ )

$(".randomRoll")
    .mouseover(function() {
               var colors = ["6F216C", "F34B0D", "C50102", "5DA537", "F1D81B"];
               var pick = Math.floor(Math.random()*5);
               var colorN = colors[pick];
               $(this).css("color", colorN); 
               //alert(colorN);
               })
    .mouseout(function() {
              $(this).css('color','black');
    });
1
  • 1
    $(this).css("color","#" + colorN); would do the trick ... Commented Jan 25, 2012 at 16:23

1 Answer 1

7

You have to add a hash (#) in front of any hex values you use in CSS. $(this).css("color","#"+colorN);

or better yet add them in your colors array.

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

2 Comments

Everyone looks over small errors such as those once in a while :) @Derek Thanks, edited.
That's not a bracket, it's a hash. Brackets are either square [] or curly {}. :)

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.