Looking at this code snippet, my goal is to have all the boxes rotate 180deg with a single click, without needing to apply different ID names:
function rotateAllBoxes() {
var boxes = document.getElementsByClassName("box");
for (var i = 0; i < boxes.length; i++) {
boxes[i].style.background = "rgba(55, 111, 172, 0.408)";
boxes[i].style.transform = "rotateZ(180deg)";
}
}
.wrap {
display: grid;
grid-template-columns: repeat(8, 1fr);
}
.box {
width: 50px;
height: 50px;
margin: 2px;
transition: all 0.5s ease-in-out;
background-color: rgba(111, 154, 201, 0.808);
}
<div class="wrap">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<button id="btn" onclick="rotateAllBoxes()">Rotate All Boxes</button>