Classlist.remove und .add funktionieren aus irgendeinem Grund nicht

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Classlist.remove und .add funktionieren aus irgendeinem Grund nicht

by Anonymous » 18 Aug 2025, 08:04

Beim Umschalten des Schalters erhalte ich diese Fehler aus der Browserkonsole. Aus irgendeinem Grund funktioniert es nicht und ich kann nicht lösen, warum dies nicht funktioniert.

Code: Select all

Uncaught TypeError: Cannot read properties of undefined (reading 'remove')
at getValue (darkModeSwitch.js:8)
at window.onload (index.html:17)

Uncaught TypeError: Cannot read properties of undefined (reading 'add')
at getValue (darkModeSwitch.js:13)
at HTMLInputElement.onclick (index.html:26)
< /code>


const body = document.body
const anchor = document.getElementsByTagName("a");
const darkModeToggle = document.getElementById('darkModeToggle');

function getValue(){
if (darkModeToggle.checked) {
body.classList.remove("lightmode")
anchor.classList.remove("lightmode")
console.log("checked")
} else{
console.log("not checked")
body.classList.add("lightmode")
anchor.classList.add("lightmode")
}
};< /code>
.lightmode{
background-color: white;
color: black;
}< /code>

[list]
[*][url=kapcsolat/kapcsolat.html?version=691]Kapcsolat[/url]
[*][url=projects/projects.html?version=691]Projektek
[*]Kezdőlap[/url]
[*]

[url=index.html?version=-23424893215]jayden.hu[/url]
[/list]

Top