I try to change the background color at on small app in html css js one snap of this is:
var myoutput = document.getElementById("output");
function roundNum(){
AlphaValue=0.4;
RedValue=255;
GreenValue=0;
BlueValue=0;
outputColor=[RedValue,GreenValue, BlueValue,AlphaValue];
return outputColor;
}
myColor=roundNum();
myColor1=myColor[0];
myColor2=myColor[1];
myColor3=myColor[2];
myColor4=myColor[3];
myoutput.style.backgroundColor ="(
rgba(" myColor1 "," myColor2 "," myColor3 "," myColor4 ");";
CodePudding user response:
Remove the ( from the first of your string, And remove ; at the end.
Change:
myoutput.style.backgroundColor ="(rgba(" myColor1 "," myColor2 "," myColor3 "," myColor4 ");";
To:
myoutput.style.backgroundColor ="rgba(" myColor1 "," myColor2 "," myColor3 "," myColor4 ")";
CodePudding user response:
You have a couple of syntax issues there. You can not define multi-line strings with double or single quotes use backticks instead. And rgba does not start with a parenthesis before r and you done need a semicolon at the end;
myoutput.style.backgroundColor = `rgba(${myColor1},${myColor2},${myColor3},${myColor4})`;
CodePudding user response:
You can use a function like this
const red = () => {
const r = 255, g = 0, b = 0, a = 1;
return `${r},${g},${b},${a}`;
}
document.body.style.backgroundColor = `rgb(${[red()]})`;
<html>
<body></body>
<html>
