I'm using document.getElementById("banner").style.backgroundColor = COLOR
to change something with cookies. I take the cookie and read it, and put the string into a variable (Ex: var backgroundcolor = "red") How would I put that into the COLOR slot as I don't know how to put a variable where COLOR is.
Heres my full code:
<script>
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var bannerlinkcolor = getCookie("bannertheme");
if (bannerlinkcolor = 0) {
var textcolor = "#000000";
var bgcolor = "#ff0000";
} else {
var textcolor = "#FFFFFF";
var bgcolor = "#800080";
}
}
</script>
<script>
document.getElementById("banner").style.color = textcolor;
document.getElementById("banner").style.backgroundColor = bgcolor;
</script>
</head>
<body>
<div id="banner" style="border:2px dotted orange; height:20px; width:100%; "><center>Check out our <a href="https://www.youtube.com/channel/UC4Eg3V26uXxWY-M639mGDrQ" target="_blank">Youtube</a> Channel! Help reinstate Net Neutrality! Visit <a href="https://www.battleforthenet.com/" target="_blank">https://www.battleforthenet.com/</a>!!<center></div>
</body>
</html>
(I left out the top because that had some Google Analytics stuff)
Heres where I store the cookie data:
function switchTheme(e) {
if (e.target.checked) {
var r = confirm("Are you sure you want to switch on light mode? (It hurts my eyes)");
if(r == true) {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light'); //add this
setCookie("bannertheme", 1, 1);
}else {
toggleSwitch.checked = false;
}
}
else {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark'); //add this
setCookie("bannertheme", 0, 1);
}
}
The toggling thing is activated by a switch
I've tried these two things and neither works:
document.getElementById("banner").style.backgroundColor = var(bgcolor);
document.getElementById("banner").style.backgroundColor = bgcolor;
document.getElementById("banner").style.backgroundColor = VARIABLE_HERE;
I need it to change the color of banner to purple