Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make the circle div in the center change color from yellow to white and vice versa on every click inside the circle?

So far I have managed to do this on single click.

I removed the lightbulb image

const div = document.getElementById("sphere");

div.addEventListener("click", e => {
    console.log("You clicked the mouse!");
    div.style.backgroundColor = "white";
})

How do I make the circle div in the center change color from yellow to white and vice versa on every click inside the circle?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can just try this way:

 const div = document.getElementById("sphere");

      div.addEventListener("click", e => {
        console.log("You clicked the mouse!");
        div.classList.toggle('active')
      })
#sphere {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        background-color: yellow;
      }

      img {
        width: 50px;
        height: 50px;
        display: block;
        margin-left: auto;
        margin-right: auto;
        padding-top: 20px;
      }

      #sphere.active {
        background-color: white;
      }
<div id="sphere">
      <img src="https://freepngimg.com/thumb/bulb/31669-1-bulb-off-transparent-image.png">
    </div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading