I have developed an HTML/CSS code inspired by the Mac/Apple calculator design. It features buttons organized in 5 rows using flexbox.
You can view my code on this codepen:
<div class="wrapper">
<div class="calheader">
<h2>Simple Calculator</h2>
</div>
<div class="calculatorbox">
<div class="calwindow">
<!-- ENTRY BOX -->
<div class="entry">
<p id="answer"></p>
</div>
<div class="entryhistory">
<p id="history"></p>
</div>
</div>
<!-- BUTTONS \\-->
<div class="calbuttons">
<div class="row">
<button id="clear" class="key topcolor" value="clear">C</button>
<button class="key topcolor" value="plusminus"><sup>+</sup>/<sub>−</sub></button>
<button class="key topcolor" value="%">%</button>
<button id="divide" class="key orange" value="/">÷</button>
</div>
<div class="row">
<button id="seven" class="key" value="7">7</button>
<button id="eight" class="key" value="8">8</button>
<button id="nine" class="key" value="9">9</button>
<button id="multiply" class="key orange" value="*">×</button>
</div>
<div class="row">
<button id="four" class="key" value="4">4</button>
<button id="five" class="key" value="5">5</button>
<button id="six" class="key" value="6">6</button>
<button id="subtract" class="key orange" value="-">−</button>
</div>
<div class="row">
<button id="one" class="key" value="1">1</button>
<button id="two" class="key" value="2">2</button>
<button id="three" class="key" value="3">3</button>
<button id="add" class="key orange" value="+">+</button>
</div>
<div class="row">
<button id="zero" class="key btnspan" value="0">0</button>
<button id="decimal" class="key" value=".">.</button>
<button id="equals" class="key orange" value="=">=</button>
</div>
</div>
</div>
</div>
Currently, I am exploring how to individually select and apply the addEventListener
function to each button element.
A different JavaScript tutorial I came across utilizes a simpler structure as shown below:
<div class="calculator-keys">
<button type="button" class="operator" value="+">+</button>
<button type="button" class="operator" value="-">-</button>
<button type="button" class="operator" value="*">×</button>
<button type="button" class="operator" value="/">÷</button>
<button type="button" value="7">7</button>
<button type="button" value="8">8</button>
<button type="button" value="9">9</button>
<button type="button" value="4">4</button>
<button type="button" value="5">5</button>
<button type="button" value="6">6</button>
<button type="button" value="1">1</button>
<button type="button" value="2">2</button>
<button type="button" value="3">3</button>
<button type="button" value="0">0</button>
<button type="button" class="decimal" value=".">.</button>
<button type="button" class="all-clear" value="all-clear">AC</button>
<button type="button" class="equal-sign operator" value="=">=</button>
</div>
The tutorial goes on to illustrate how to interact with these elements using JavaScript:
const keys = document.querySelector('.calculator-keys');
keys.addEventListener('click', (event) => {
const { target } = event;
console.log('digit', target.value);
});
In this context, the querySelector
method is utilized to target all children within the calculator-keys
class for interaction.
In my situation, I was able to implement this functionality only for the first row of buttons. If I proceed to use querySelectorAll
, would it be necessary to employ .map()
, .forEach()
, or some other technique to effectively utilize addEventListener
for each individual button?