Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

116
Vistas
Change IMG URL with MutationObserver

I've made a dark mode toggle button for my demo website that adds a class to change the background color. The button works, but I want to swap out the image the button is using depending on if the class is present. It's not working. I think I've messed up MutationObserver somehow, can anyone help?

Javascript

let buttonIMG = document.getElementById("darkButtonIMG");
const observer = new MutationObserver(darkImage);
observer.observe(buttonIMG, {
    attributes: true
});

function darkImage() {
    let buttonIMG = document.getElementById("darkButtonIMG");
    let buttonSRC = buttonIMG.hasAttribute("dark");

    if (buttonSRC === true) {
        buttonIMG.setAttribute("src", "images/Sun_Icon.png");
    } else {
        buttonIMG.setAttribute("src", "images/Moon_Icon.png");
    }
}

HTML

            <nav>
                <div class="row">
                    <button class="buttonHide" id="hamburgerBtn">&#9776;</button>
                        <ul id="navOpen">
                        ...
                        <li><button id="darkButton" type="button" class=""><img id="darkButtonIMG"src="images\Moon_Icon.png" alt="Dark mode icon"></button></li>
                        </ul>
                </div> <!-- End of Navbar Row -->
            </nav>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I want to swap out the image the button is using depending on if the class is present.

I'm assuming that when you click the button you're adding class dark to it.

In your callback method you're checking for the presence of dark attribute, but you should check for the presence of a class instead.

let buttonSRC = buttonIMG.classList.contains("dark");
about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to setAttribute for check hasAttribute

let buttonIMG = document.getElementById("darkButtonIMG");
const observer = new MutationObserver(darkImage);
observer.observe(buttonIMG, {
    attributes: true
});

function darkImage() {
    let buttonIMG = document.getElementById("darkButtonIMG");
    let buttonSRC = buttonIMG.hasAttribute("dark");

    if (buttonSRC === true) {
        buttonIMG.setAttribute("src", "images/Sun_Icon.png");
        buttonIMG.removeAttribute("dark");
    } else {
        buttonIMG.setAttribute("src", "images/Moon_Icon.png");
        buttonIMG.setAttribute("dark", "dark");
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda