Change your theme color using js

anjit pariyar
Apr 7, 2021

html meta theme-color only supported by chrome android till now.

the color set the background color of your top part of browser

<meta name="theme-color" content="#4285f4">

but how do we switch it from white to dark and vice-versa for darkmode and lightmode?

let meta = document.getElementsByTagName("META");

meta = [...meta];
meta.map((elem) => {

if (elem.name === "theme-color") meta = elem;

});
if (getCookie("theme") === "dark") {
meta.content = "#222";
}
else {
meta.content = "#fff";
}
//Theme Toggle
var gettheme = document.querySelector(".theme-switch");
getTheme.addEventListener("click", () => {
if (getCookie("theme") === "light") { meta.content = "#222";
}
else {
meta.content = "#fff";
}
});
Change your theme color using js
Change your theme color using js

--

--