My default Leaflet popups have a background color of white. When a user turns on dark mode, I want them all to turn black.
I'm looping over all of the popups, but for some reason they're not changing color. I've tried adding a custom class to the popup but that's not working either.
What am I missing to change the color of all popup wrappers dynamically?
.leaflet-popup-content-wrapper {
background: white;
}
var wrapper = document.getElementsByClassName('leaflet-popup-content-wrapper');
for (var i = 0; i < wrapper.length; i++) {
wrapper[i].style.backgroundColor = 'black';
}
Edit
I realized through XaC's answer below that I need to change all unopened popups. How would I go about this?
Updated Code
Here's the code I'm using. Switching classes works if I just use L.popup()
,as the popups are open on the map. However, when using marker.bindPopup()
it fails and the classes don't switch.
Nothing gets printed to the console when looping through either. Basically it looks like the popups don't even exist until the marker is clicked.
CSS
.light .leaflet-popup-content-wrapper {
background: black;
}
.dark .leaflet-popup-content-wrapper {
background: white;
}
Placing the markers
marker.bindPopup(popupContents(item), {
closeButton: false,
className: 'light'
}).addTo(layer);
Switching the classes
document.addEventListener('dark-mode-updated', event => {
if (event.detail.text === 1) {
var popups = document.getElementsByClassName('light');
while (popups.length > 0) {
// nothing prints here when using bindPopup() UNLESS a popup is open
console.log(popups);
popups[0].classList.replace('light','dark');
}
}
}