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

198
Views
Why is my js returning an empty string when I check the background colour of an element?

I have an event listener which listens for a click on an element, and then runs a function that checks if the background colour of the element is white. Here is my code:

setting.addEventListener("click", (e) => {
       if (e.target.style.backgroundColor == "white") {
           e.target.style.backgroundColor = "black";
       } else {
           console.log(e.target.style.backgroundColor);
           e.target.style.backgroundColor = "white";
       }
});

My intention is that the element will first turn black, since its background colour is white.

Here is my css for this element:

.toggle {
    display: inline-block;
    height: 20px;
    width: 20px;
    background-color: white;
    border-radius: 50%;
    position: absolute;
    right: 300px;
    outline: 3px solid black;
    border: 3px solid white;
}

The console log in the js logs this message:

<empty string>

Why is this happening, and how should I fix it?

about 4 years ago · Juan Pablo Isaza
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!