JavaScript How to switch between two different colors with one button using javascript switch between different chrome windows mac,switch between different windows

Im trying to change between two colors when I click on the event listener using JavaScript. When I trigger the click event, it changes the color to black. When I click on it again, I want it to change back to the default, which is white. I think that an if statement is involved, but Im not sure how the logic would work in this case.

<!DOCTYPE html>
<html>
<head>
  <title>Light Switch</title>
  <link rel="stylesheet" target='_blank' href="light.css" />

</head>

<body id="body">

<button id="submit">Submit
  <label id="switch">
    <input type="checkbox" checked>
    <span class="slider"></span>
  </label>
</button>

<script src="light.js"></script>
</body>
</html>


function start() {
  var submit = document.getElementById("submit");
  submit.addEventListener("click", toggle);
};

function toggle() {
  var color = document.getElementById("body");
  color.style.backgroundColor = "black";
};


start();
Answer:1

You can use toggle function on the classList of that element. Add a class and toggle it.

function start() {
  var submit = document.getElementById("submit");
  submit.addEventListener("click", toggle);
};

function toggle() {
  var color = document.getElementById("body");
  color.classList.toggle('black');
};


start();
.black {
  background-color: black;
}
<body id="body">
   <button id="submit">Submit
      <label id="switch">
          <input type="checkbox" checked>
          <span class="slider"></span>
      </label>
   </button>
</body>
Answer:2

Instead of manipulating backgroundColor, You can create CSS class and add/remove it to the element using classList property

function start() {
  var submit = document.getElementById("submit");
  submit.addEventListener("click", toggle);
};

function toggle() {
  var color = document.getElementById("body");
  color.classList.toggle("black");
};


start();
.black {
  background-color: black;
}
<div id="body">

  <button id="submit">Submit
  <label id="switch">
    <input type="checkbox" checked>
    <span class="slider"></span>
  </label>
</button>


</div>
Answer:3

Do like this, i think it work for you...

var background = document.getElementById(id).style.background;
var btn = addEventListener("click", function (){
    if(background = "rgb(255,145,0)"){
        document.getElementById(id).style.background = "red";
    }
    else
    {
        document.getElementById(id).style.background = "white";
    }
});
Answer:4

I realize that this is improper syntax, but why is JavaScript ignoring the first number and giving a response instead of breaking? let myArray = [1,2,3,4] myArray[0,1] // 2 myArray[1,3] // 4 myArray[...

I realize that this is improper syntax, but why is JavaScript ignoring the first number and giving a response instead of breaking? let myArray = [1,2,3,4] myArray[0,1] // 2 myArray[1,3] // 4 myArray[...

  1. passing values in java
  2. passing values in url
  3. passing values in python
  4. passing values in javascript function
  5. passing values in c
  6. passing values in url php
  7. passing values in query string c#
  8. passing values in php
  9. passing values in python functions
  10. passing values in javascript
  11. passing values in onclick of javascript
  12. passing values in hyperlink html
  13. passing values in intent
  14. passing values in react
  15. passing values in url javascript
  16. passing values in html
  17. passing values in vba
  18. passing values in postman
  19. passing values in ajax
  20. passing values in angular

I have the following code to listen to click events on div elements. HTML: <div id="container"> <div id="1" class="square"></div> <div id="2" class="square"></div&...

I have the following code to listen to click events on div elements. HTML: <div id="container"> <div id="1" class="square"></div> <div id="2" class="square"></div&...

I was searching an issue about a delay in my Pomodoro clock (FreeCodeCamp project) that I discovered when I noticed my test session of one minute should have been done by now. I checked back and it ...

I was searching an issue about a delay in my Pomodoro clock (FreeCodeCamp project) that I discovered when I noticed my test session of one minute should have been done by now. I checked back and it ...

Hya, I have the below setup: App.Component.Ts contents carForm: FormGroup; constructor( private fb: FormBuilder ) { this.carForm= this.fb.group({ name: '', type: '', ...

Hya, I have the below setup: App.Component.Ts contents carForm: FormGroup; constructor( private fb: FormBuilder ) { this.carForm= this.fb.group({ name: '', type: '', ...