Home > Software design >  Toggle Darkmode in HTML
Toggle Darkmode in HTML

Time:01-03

i want to make some code with javascript, html and css to toggle darkmode and nightmode. But i dont know how to do it

I Have a button with class btn-night (line 22) to switch from light mode to dark mode

I want to change "ralue-theme.min.css" in line 14 to ralack-theme.min.css with javascript

so, when i click the button in line 22, i want it to be able that button to change the stylesheet rel in line 14 from "/dist/css/skins/ralue-theme.min.css" to "/dist/css/skins/ralack-theme. min. css"

*I'm using EJS btw

CodePudding user response:

You should add an onclick event in line 22 and define a function like switchMode in your js file.

You can handle this situation with add and delete classes.

Also please use snippet for codes next time don't send photos like this.

CodePudding user response:

You can use removeAttribute and setAttribute for change the link src. See example:

HTML FILE:

<!--Line 14 of HTML-->
<link rel="stylesheet" href="/dist/css/skins/ralue-theme.min.css" id="link" />
<!--Here we add a id="link"-->

<!--Line 22 of html-->
<a id="nightMode"  />

JS FILE:

let nightButton = document.querySelector("#nightMode");
let styleSheet = document.querySelector("#link");

nightButton.addEventListener("click",() => {
styleSheet.removeAttribute("src")
styleSheet.setAttribute("src","dist/css/skins/ralack-theme. min. css") 
})

See element.setAttribute documentation and element.removeAttribute documentation

  • Related