Mozar Mozar - 1 year ago 89
Javascript Question

Randomly colored circle

I am working on a little project which assigns a random color to this circle from all the possible colors.

I am trying to achieve this by assigning a random number to each RGB value. However, It doesn't seem to work this way. I also tried to setAttribute() way to assign it that way but also no luck. Please have a look:

<!DOCTYPE html>
<html lang="en">
<title>Beginning JavaScript</title>

border:1px solid black;
border-radius: 50%;

<div id="circle"></div>

var circle = document.getElementById('circle');
var value1 = Math.floor(Math.random() * 256);
var value2 = Math.floor(Math.random() * 256);
var value3 = Math.floor(Math.random() * 256); = "rgb(value1,value2,value3)"

Answer Source

You need to concatenate the generated values into the style statement: = "rgb(" + value1 + "," + value2 + "," + value3 + ")";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download