3

I'd like to change the text color using the following code, but I get an error: Uncaught syntax: unexpected string when .each is called. I'm not sure what's wrong.

function random_rgb() {
  colors = ['8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
  r = colors.eq(Math.floor(Math.random * 8));
  g = colors.eq(Math.floor(Math.random * 8));
  b = colors.eq(Math.floor(Math.random * 8));
  return '#' + r + g + b;
};

$(document).ready({

  $("span.number").each(function(){
    this.style.color = random_rgb();
  });

});

UPDATE

I've created a jsfiddle (forgive me I'm not great w/ this app) https://jsfiddle.net/yrnqr566/

The color turns up black every time.

3
  • 1
    What's your HTML like? Got a fiddle to show off this error? Commented Mar 24, 2016 at 13:17
  • See update please. Commented Mar 24, 2016 at 13:20
  • 1
    Math.random is a function, you need to use parenthesis to call it : Math.random() Commented Mar 24, 2016 at 13:26

3 Answers 3

4

You have a couple of errors, see this fiddle for the fixes; https://jsfiddle.net/yrnqr566/10/

$(function() {...}); is a better alternative to document.ready...

Also, you need to use Math.random() with open/close brackets, as this is a function not a property.

Third, you were using .eq() on an array. [] is the correct syntax.

function random_rgb() {
  colors = ['8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
  r = colors[Math.floor(Math.random() * 8)];
  g = colors[Math.floor(Math.random() * 8)];
  b = colors[Math.floor(Math.random() * 8)];
  return '#' + r + g + b;
};

$(function() {
  $('span.number').each(function() {
    this.style.color = random_rgb();
  });
});
Sign up to request clarification or add additional context in comments.

Comments

2

Guess you need something like this, you had a few errors in your random_rgb() function.
Also jquery way would be $(this).css('color',random_rgb());

function random_rgb() {
  colors = ['8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
  r = colors[Math.floor(Math.random() * 8)];
  g = colors[Math.floor(Math.random() * 8)];
  b = colors[Math.floor(Math.random() * 8)];
  return '#' + r + g + b;
}

$("span.number").each(function() {
  this.style.color = random_rgb();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="number">Some Long Text</span>
<span class="number">Some Long Text</span>
<span class="number">Some Long Text</span>
<span class="number">Some Long Text</span>
<span class="number">Some Long Text</span>
<span class="number">Some Long Text</span>
<span class="number">Some Long Text</span>

Comments

0

shoud return something like :return rgb(255, 0, 0) or try this:

$(document).ready(function () {

  $('div').css('background',randrgb());
})

1 Comment

Sorry, the function says rgb but I want a hex value.

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.