JS - setting the background color of the background using a variable

Well, basically, one of my comrades practiced JS, and he had the idea of ​​a test base site. So I said that we will have a race to finish it. On this we both encountered a mistake. We created the color in JS. However, when we need to output, this will not work. I have it.

document.getElementById("outputColor").style.backgroundColor=currentColor; 

If the current color is made using this

 part1 = Math.floor(Math.random() * (255 - 0 + 1)) + 0; part2 = Math.floor(Math.random() * (255 - 0 + 1)) + 0; part3 = Math.floor(Math.random() * (255 - 0 + 1)) + 0; currentColor = "\"rgb (" + part1 + ", " + part2 + ", " + part3 + ")\""; 

Putting the current color in "" means that it expects the value of currentColor. Not the actual value of the variable.

Hope this makes sense. Is this possible, or are we barking the wrong tree?

thanks

Edit: It already has the css style associated with it

 #outputColor { height: 100px; width: 100px; background-color: rgb(0,0,0); } 

Edit: Allowed, Solution

 currentColor = "rgb(" + part1 + ", " + part2 + ", " + part3 + ")"; 

Thanks everyone!

+4
source share
3 answers

Too many double quotes, use this:

 currentColor = "rgb(" + part1 + ", " + part2 + ", " + part3 + ")"; 
+3
source
 currentColor = "rgba(" + part1 + ", " + part2 + ", " + part3 + ",0)"; 
+1
source
 currentColor = "rgb(" + part1 + ", " + part2 + ", " + part3 + ")"; // RGB 

OR Using Hex Format

 currentColorHex="#"+(part1).toString(16)+(part2).toString(16)+(part3).toString(16); 

Demo.

+1
source

All Articles