Home > OS >  changing the background of a page with a variable
changing the background of a page with a variable

Time:10-11

So far, I have been able to make a variable that will store the hexcode, but I can't figure out how to use that variable in order to change the background color to that color. This is my code so far:

document.body.style.backgroundColor = 'rgb(${redhex}, ${greenhex}, ${bluehex})';

where redhex, greenhex, and bluehex are all variables that the user can change for the RGB. I think that part of the issue might be that I have another variable in my code named rgb?

CodePudding user response:

You might need to use a template string to interpolate other values inside the string. So in your case, it might be

document.body.style.backgroundColor = `rgb(${redhex}, ${greenhex}, ${bluehex})`;

CodePudding user response:

I believe that you could also use this website https://www.rgbtohex.net/

It is a tool I use some times to convert RGB into simple hex values this will replace

rgb(${redhex}, ${greenhex}, ${bluehex}) with ${rgbHexCode}

Resulting in document.body.style.backgroundColor = ${rgbHexCode};

CodePudding user response:

If I understand you, the variable is hexcode, but then, rgb doesn't take hex values. Rgb takes base 10 values from 0 to 255. Be sure to check if you are sending the right type of value

  • Related