when i push on button the background of <div>1</div> is yellow, when i push it second time background of <div>2</div> is yellow and <div>1</div> is white but when i do it third time <div>3</div> background wont change why is this?
<script lang="javascript">
var bool = false;
function ChangeColor(){
var first = document.getElementById("first");
var second = document.getElementById("second");
var third = document.getElementById("third");
if (bool == false){
first.style.background = ("yellow");
bool = true;
}else if (bool == true){
first.style.background = ("white");
second.style.background = ("yellow");
}else if (bool == true){
second.style.background = ("white");
third.style.background = ("yellow");
}
}
</script>
</head
<body
<div id="first">1</div
<div id="second">2</div
<div id="third">3</div
<button onclick="ChangeColor()"change</button
</body
</html>
Because the second if evaluated to true, the third if never gets checked, it is skipped over. Specifically, javascript doesnt recognize a concept of âelseifâ, it is just you tacking an if statement into the else block. You could write the same block as:
Because when bool is true your program only runs into the 2nd condition, never the 3rd. You might use a counter variable instead like so:
var colorSwitch = 0
var first = document.getElementById('first')
var second = document.getElementById('second')
var third = document.getElementById('third')
function changeColor () {
// Perform actions depending on the value
// of the color switch
switch (colorSwitch) {
case 0:
first.style.background = ('yellow')
break
case 1:
first.style.background = ('white')
second.style.background = ('yellow')
break
case 2:
second.style.background = ('white')
third.style.background = ('yellow')
break
default:
// Do nothing?
}
// Increment the color switch
colorSwitch++
}
<script lang="javascript">
function ChangeColor(){
var colorSwitch = 0
var first = document.getElementById("first");
var second = document.getElementById("second");
var third = document.getElementById("third");
switch (colorSwitch) {
case 0:
first.style.background = ("yellow");
break;
case 1:
first.style.background = ("white");
second.style.background = ("yellow");
break;
case 2:
second.style.background = ("white");
third.style.background = ("yellow");
break;
}
colorSwitch++;
}
</script>
</head>
<body>
div id="first">1</div>
div id="second">2</div>
div id="third">3</div>
button onclick="ChangeColor()">change</button>
</body>
</html>
You declare the colorSwitch variable inside the changeColor() function, so each time the function gets called, youâre declaring it anew with the initial value of 0 (and discarding it after the function execution finished). You have to pull it out of the scope of that function to keep it across multiple function calls.
PS: I see you have your JS in the head⌠in the majority of cases itâs better placed at the very end of the body. This way, the DOM is fully available to the JS and you can pull the .getElementById() queries out of the function too, so that you donât have to query for those elements anew each time the function gets called (like in my snippet above).
var colorSwitch = 0
var first = document.getElementById("first");
var second = document.getElementById("second");
var third = document.getElementById("third");
its inside the function i did it outside and it works now