when first time the button clicked, the fade animation(opacity changing) works. But for next all clicks it does'nt work anymore. When i do debug it shows it's working everytime. But in real why doesn't?
let colorArray = ['red','yellow','green','teal']
let index = 0
function imageChanger(){
document.querySelector('.container').id = ''
if(index == colorArray.length-1) index = 0
else index++
document.querySelector('.container').style.backgroundColor = colorArray[index]
document.querySelector('.container').id = 'fade'
}
.container{
height: 300px;
max-width: 400px;
background-color: teal;
}
#fade{
animation: fade 5s;
}
@keyframes fade{
from{opacity:.4 }
to{opacity: 1}
}
<button onclick="imageChanger()">Change</button>
<div class="container" id=""></div>