+ 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
14 Antworten
+ 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?
+ 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
+ 3
How TO - Toggle Dark Mode
https://www.w3schools.com/howto/howto_js_toggle_dark_mode.asp
+ 3
SoloProg don't forget to declare the type of the variable:
const darkmodeisbetterlol = ()=>{
const darkmode = document.body;
darkmode.classList.toggle("dark-mode");
}
+ 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";
}
}
+ 2
Your Mom , yes.
+ 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;
}
+ 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;
}
+ 1
K thanks
+ 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 = "";
}
}
0
What do i put in my html for the document.getElementById(“main”)?
0
So Solo i just change it to contains?
0
Yes, but when the screen is black and you click it againandbthe screen turns white the text stays white, thats the problem
0
Actually you can use a checkbox there and check for the checked state of the checkbox