Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!