Task:
We want to create nine buttons enclosed in a div, laid out so they form a 3 X 3 grid. Each button has a distinct label from 1 to 9, and the labels on the outer buttons must rotate in the clockwise direction each time we click the middle button.
Complete the code in the editor so that it satisfies the following criteria:
- Initial State. The initial layout looks like this:
- Element IDs. Each element in the document must have an
id
, specified below:- The button container div‘s
id
must bebtns
. - The initial
innerHTML
labels must have the following buttonid
s: - Styling. The document’s elements must have the following styles:
- The button container div‘s
- The
width
ofbtns
is 75%, relative to the document body’s width. - Each button (i.e.,
btn1
throughbtn9
) satisfies the following:- The
width
is 30%, relative to its container width. - The
height
is48px
. - The
font-size
is24px
.
- The
- The
- Behavior. Each time
btn5
is clicked, theinnerHTML
text on the grid’s outer buttons (i.e.,bt1
,btn2
,btn3
,btn4
,btn6
,btn7
,btn8
,btn9
) must rotate in the clockwise direction. Do not update the buttonid
‘s.
The .js
and .css
files are in different directories, so use the link tag to provide the CSS file path and the script tag to provide the JS file path:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/buttonsGrid.css" type="text/css"> </head>
<body>
<script src="js/buttonsGrid.js" type="text/javascript"></script> </body>
</html>
Explanation
Initially, the buttons look like this:

After clicking btn5
time, they look like this:
After clicking btn5
more time (for a total of clicks), they look like this:
Solution:
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Buttons</title>
<link rel="stylesheet" href="css/buttonsGrid.css" type="text/css"> </head>
<body>
<div id="btns" class="buttonContainer">
<button id="btn1" class="buttonClass">1</button>
<button id="btn2" class="buttonClass">2</button>
<button id="btn3" class="buttonClass">3</button>
<button id="btn4" class="buttonClass">4</button>
<button id="btn5" class="buttonClass">5</button>
<button id="btn6" class="buttonClass">6</button>
<button id="btn7" class="buttonClass">7</button>
<button id="btn8" class="buttonClass">8</button>
<button id="btn9" class="buttonClass">9</button>
</div>
<script src="js/buttonsGrid.js" type="text/javascript"></script> </body>
</html>
CSS:
.buttonContainer {
width: 75%;
}
.buttonContainer > .buttonClass {
width: 30%;
height: 48px;
font-size: 24px;
}
JAVASCRIPT:
document.addEventListener("DOMContentLoaded",()=>{
let btn_ids=[1,2,3,6,9,8,7,4];
let btn_values=[1,2,3,6,9,8,7,4];
const btn5=document.getElementById("btn5");
btn5.addEventListener("click",()=>{
btn_values.unshift(btn_values.pop());
for(let i=0;i<btn_ids.length;i++)
document.getElementById("btn"+btn_ids[i]).innerHTML=btn_values[i];
});
});