<div class="content">
<?php
for ($i=0; $i < 5; $i++) {
echo '<div class="foo">
</div>';
}
?>
<script>
var foo= document.getElementsByClassName('foo');
for (var i = 0; i < foo.length; i++) {
foo[i].addEventListener("click", function(){
this.style.backgroundColor = "lightgreen";
});
}
</script>
I have created one div class called "content" and inside that content div there are five divs generated using php for loop. I want to change the divs "foo" background color to light green which is achieved already, but my concern is how to change the background color property again to white?
togglea class insteadbackground-color: lightgreenand toggle this class on the element instead:this.classList.toggle('your_class_name').