Recently, I have been working on implementing a 'night mode' feature for a website.
However, every time I attempt to execute the script, an error message pops up:
"SyntaxError: Expected token ']'" on line 9.
The problematic line in question reads as follows:
this.pageElements[0] = ['element': document.body, 'background': ['day': '#f1f1f1', 'night': '#0e0e0e']];
I cannot seem to figure out why it is not functioning properly.
Below is the complete code snippet:
window.nightMode = {};
//Functions
nightMode.init = function () {
this.pageElements = [];
//Format: ['element': Element, 'background': ['day': Day background color, 'night': Night background color], 'text': ['day': Day text color, 'night': Night text color]];
this.pageElements[0] = {
'element': document.body,
'background': {
'day': '#f1f1f1',
'night': '#0e0e0e'
}
};
this.pageElements[1] = {
'element': document.getElementById('yt-masthead-container'),
'background': {
'day': '#fff',
'night': '#000'
}
};
this.pageElements[2] = {
'element': document.getElementById('search-btn'),
'background': {
'day': '#fff',
'night': '#000'
}
};
this.pageElements[3] = {
'element': document.getElementById('masthead-search-terms'),
'background': {
'day': '#fff',
'night': '#000'
},
'text': {
'day': '#666',
'night': '#999'
}
};
this.isNight = false;
}
nightMode.setNight = function () {
for (var i = 0; i < this.pageElements.length; i++) {
this.pageElements[i].element.style.background = this.pageElements[i].background.night
if (this.pageElements[i].text) {
this.pageElements[i].element.style.color = this.pageElements[i].text.night
}
}
alert('Night mode');
this.isNight = true;
}
nightMode.setDay = function () {
for (var i = 0; i < this.pageElements.length; i++) {
this.pageElements[i].element.style.background = this.pageElements[i].background.day
if (this.pageElements[i].text) {
this.pageElements[i].element.style.color = this.pageElements[i].text.day
}
}
alert('Day mode');
this.isNight = false;
}
nightMode.toggle = function () {
if (this.isNight) {
this.setDay();
} else {
this.setNight();
}
}
//Startup Code
nightMode.init();
nightMode.toggle();