I'm currently working on a JavaScript calculator project and facing challenges in understanding how to create variables for storing targeted DOM elements, input/output values, and adding event listeners to retrieve data from buttons upon clicking.
I am planning to utilize the function(event) method to display the button value (whether it's a number or operation) on the screen. I also intend to use event.target.dataset.num to extract and store the value as a new variable to be added to the screen display.
Here is a snippet of my current code:
//3.
//Changing colors of operation buttons
//Setting color for Multiplication
const multiplyColor = document.getElementById('multiply')
multiplyColor.style.backgroundColor = "green"
//Setting color for Division
const divideColor = document.getElementById('divide')
divideColor.style.backgroundColor = "red"
//Setting color for Subtraction
const subtractColor = document.getElementById('subtract')
subtractColor.style.backgroundColor="blue"
//Setting color for Addition
const addColor = document.getElementById('add')
addColor.style.backgroundColor="yellow"
//Changing font color of numbers to blue for better visibility
const num1 = document.getElementById('number1')
num1.style.color="blue"
... (similar lines for numbers 2-9)
//Customizing clear button style
const clearBtn = document.getElementById('clear')
clearBtn.style.color="white"
clearBtn.style.backgroundColor="black"
// Inserting `memoryStoreButton` before the `clear` button:
var memoryStoreBtn = document.createElement("BUTTON");
memoryStoreBtn.innerHTML = "MS";
clear.before(memoryStoreBtn);
//Inserting `memoryClearButton` before `memoryStoreButton`
var memoryClearBtn = document.createElement("BUTTON");
memoryClearBtn.innerHTML = "MC";
memoryStoreBtn.before(memoryClearBtn);
//Inserting `memoryRestoreButton` before `memoryClearButton`
var memoryRestoreBtn= document.createElement("BUTTON");
memoryRestoreBtn.innerHTML = "MR";
memoryClearBtn.before(memoryRestoreBtn);
const btnClick = document.querySelector(".btn8 .btn-grey");
/*
btnClick.addEventListener('click', function(event){
})
*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.calculator8 {
flex: 0 0 40%;
}
.screen8 {
width: 100%;
font-size: 7rem;
padding: 0.5rem;
background: rgb(41,41,56);
color: white;
border:none;
}
.buttons8 {
display: flex;
flex-wrap: wrap;
transition: all 0.5s linear;
}
button {
flex:0 0 25%;
border: 1px solid black;
padding: 0.25rem 0;
transition: all 2s ease;
}
.btn-grey {
background: rgb(224,224,224);
}
.btn8 {
font-size: 4rem;
}
<section class="calculator8">
<h1> Calculator 8 </h1>
<form>
<input type="text" name="" id="numberBox" class="screen8">
</form>
<div class="buttons8">
<!-- operation buttons -->
<button id="multiply" type="button" class="btn8 btn-mul" data-num="*">*</button>
<button id="divide" type="button" class="btn8 btn-div" data-num="/">/</button>
<button id="subtract" type="button" class="btn8 btn-sub" data-num="-">-</button>
<button id="add" type="button" class="btn8 btn-add" data-num="+">+</button>
<!-- number buttons -->
<button id="decimal" type="button" class="btn8 btn-grey" data-num=".">.</button>
<button id="number9" type="button" class="btn8 btn-grey" data-num="9">9</button>
<button id="number8" type="button" class="btn8 btn-grey" data-num="8">8</button>
<button id="number7" type="button" class="btn8 btn-grey" data-num="7">7</button>
<button id="number6" type="button" class="btn8 btn-grey" data-num="6">6</button>
<button id="number5" type="button" class="btn8 btn-grey" data-num="5">5</button>
<button id="number4" type="button" class="btn8 btn-grey" data-num="4">4</button>
<button id="number3" type="button" class="btn8 btn-grey" data-num="3">3</button>
<button id="number2" type="button" class="btn8 btn-grey" data-num="2">2</button>
<button id="number1" type="button" class="btn8 btn-grey" data-num="1">1</button>
<button id="number0" type="button" class="btn8 btn-grey" data-num="0">0</button>
<button id="equals" type="button" class="btn8 btn-grey">=</button>
<button id="clear" type="button" class="btn8 btn-grey">C</button>
</div>
</section>