Dark Mode | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Dark Mode

2 questions, first i want it to not be a one time click if you get what i mean. And how would i make the css when you click it the button insets? https://code.sololearn.com/WdtITbzZWQwU/?ref=app

16th Mar 2022, 8:45 PM
Junior
Junior - avatar
14 Answers
+ 3
Your Mom, Is it really difficult to guess to add the desired style to "else", you've been studying on SoloLearn for quite a long time, haven't you?
16th Mar 2022, 11:24 PM
Solo
Solo - avatar
+ 3
function togglemode(){ var main = document.getElementById("main"); if(!main.classList.contains("dark")){ main.classList.add("dark") main.classList.remove("light") } else { main.classList.add("light") main.classList.remove("dark") } } hope this helps
16th Mar 2022, 9:01 PM
Fu Foy
Fu Foy - avatar
16th Mar 2022, 11:02 PM
SoloProg
SoloProg - avatar
+ 3
SoloProg don't forget to declare the type of the variable: const darkmodeisbetterlol = ()=>{ const darkmode = document.body; darkmode.classList.toggle("dark-mode"); }
16th Mar 2022, 11:40 PM
Solo
Solo - avatar
+ 2
Your Mom, Fu Foy probably wants to tell you that you would change your condition to: if(darkmode.classList.contains("dark-mode")) {...} Although you can use the .toggle() method you already know: function darkmodeisbetterlol() { let darkmode = document.body; if(darkmode.classList.toggle("dark-mode")){ document.getElementById("colors") .style.color = "white"; } }
16th Mar 2022, 10:36 PM
Solo
Solo - avatar
+ 2
Your Mom , yes.
16th Mar 2022, 11:08 PM
Solo
Solo - avatar
+ 2
// try it js darkmodeisbetterlol = () => { let darkmode = document.body; darkmode.classList.toggle("dark-mode"); } css body { background-color: white; color: black; } .dark-mode { background-color: black; color: white; } button { /*background-color: transparent;*/ height: 75px; width: 250px; font-size: 30px; font-family: sans-serif; border: 3px outset black; border-radius: 5px; }
16th Mar 2022, 11:14 PM
SoloProg
SoloProg - avatar
+ 2
// try it v.2 :) Js darkmodeisbetterlol = () => { let darkmode = document.body; darkmode.classList.toggle("dark-mode"); let darkmodeBtn = document.getElementById("colors"); darkmodeBtn.classList.toggle("dark-mode-button"); } css body { background-color: white; color: black; } .dark-mode { background-color: black; color: white; } button { /*background-color: transparent;*/ height: 75px; width: 250px; font-size: 30px; font-family: sans-serif; border: 3px outset black; border-radius: 5px; background-color: white; color: black; } .dark-mode-button { border: 3px outset white; background-color: black; color: white; }
16th Mar 2022, 11:31 PM
SoloProg
SoloProg - avatar
+ 1
K thanks
16th Mar 2022, 11:34 PM
Junior
Junior - avatar
+ 1
Try this, Just using if/else statement in javascript :-) function toggle_mode(){ var x = document.getElementById("anydiv"); if (x.style.background == "") { x.style.background = "black"; } else { x.style.background = ""; } }
17th Mar 2022, 4:02 PM
Shailendra Maurya
0
What do i put in my html for the document.getElementById(“main”)?
16th Mar 2022, 9:07 PM
Junior
Junior - avatar
0
So Solo i just change it to contains?
16th Mar 2022, 11:06 PM
Junior
Junior - avatar
0
Yes, but when the screen is black and you click it againandbthe screen turns white the text stays white, thats the problem
16th Mar 2022, 11:12 PM
Junior
Junior - avatar
0
Actually you can use a checkbox there and check for the checked state of the checkbox
18th Mar 2022, 12:40 AM
TrUtH
TrUtH - avatar