Так что я пытаюсь сделать калькулятор, но меня сильно озадачило. Я хочу записать нажатие моих кнопок, но в верхнюю часть калькулятора, а также с помощью прослушивателя событий. Я хочу использовать функцию (событие) для отображения значения кнопки (количество операций) на экране, а также event.target.dataset.num, чтобы вернуть значение и сохранить его как новую переменную, добавленную в значение экрана.
Мой код сейчас очень грубый, просто нужно некоторое руководство.
//3.
//Changing colors of opertion colors
//Multiply Color
const colorMultiply = document.getElementById('multiply')
colorMultiply.style.backgroundColor = "green"
//Divide Color
const colorDivide = document.getElementById('divide')
colorDivide.style.backgroundColor = "red"
//subtract color
const colorSubtract = document.getElementById('subtract')
colorSubtract.style.backgroundColor="blue"
//add color
const colorAdd = document.getElementById('add')
colorAdd.style.backgroundColor="yellow"
//change font of numbers to blue (I did it like this incase anyone wants to color of a single number)
//Makes it easier
const number1 = document.getElementById('number1')
number1.style.color="blue"
const number2 = document.getElementById('number2')
number2.style.color="blue"
const number3 = document.getElementById('number3')
number3.style.color="blue"
const number4 = document.getElementById('number4')
number4.style.color="blue"
const number5 = document.getElementById('number5')
number5.style.color="blue"
const number6 = document.getElementById('number6')
number6.style.color="blue"
const number7 = document.getElementById('number7')
number7.style.color="blue"
const number8 = document.getElementById('number8')
number8.style.color="blue"
const number9 = document.getElementById('number9')
number9.style.color="blue"
const number0 = document.getElementById('number0')
number0.style.color="blue"
const decimal = document.getElementById('decimal')
decimal.style.color="blue"
//Changing color of the clear button
const clear = document.getElementById('clear')
clear.style.color="white"
clear.style.backgroundColor="black"
///////////////////////////////////////////////////////////////////////////////////////////////////////
// Then we want to insert `memoryStoreButton` before the `clear` button:
var memoryStoreButton = document.createElement("BUTTON");
memoryStoreButton.innerHTML = "MS";
clear.before(memoryStoreButton); //puts button before clear
// Then we want the `memoryClearButton` before `memoryStoreButton`
var memoryClearButton = document.createElement("BUTTON");
memoryClearButton.innerHTML = "MC";
memoryStoreButton.before(memoryClearButton);//puts button before clear
// and finally, the `memoryRestoreButton` before `memoryClearButton`
var memoryRestoreButton = document.createElement("BUTTON");
memoryRestoreButton.innerHTML = "MR";
memoryClearButton.before(memoryRestoreButton);//puts button before clear
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//I need variables that will store targeted DOM elements, input/outputs
//And I need to add an event listener to retrieve data from all buttons when they are clicked.
const buttonOne = document.getElementById('number1');
buttonOne.addEventListener('click', function(){
console.log(1);
});
const buttonTwo = document.getElementById('number2');
buttonTwo.addEventListener('click', function(){
console.log(2);
});
const buttonThree = document.getElementById('number3');
buttonThree.addEventListener('click', function(){
console.log(3);
});
const buttonFour = document.getElementById('number4');
buttonFour.addEventListener('click', function(){
console.log(4);
});
const buttonFive = document.getElementById('number5');
buttonFive.addEventListener('click', function(){
console.log(5);
});
const buttonSix = document.getElementById('number6');
buttonSix.addEventListener('click', function(){
console.log(6);
});
const buttonSeven = document.getElementById('number7');
buttonSeven.addEventListener('click', function(){
console.log(7);
});
const buttonEight = document.getElementById('number8');
buttonEight.addEventListener('click', function(){
console.log(8);
});
const buttonNine = document.getElementById('number9');
buttonNine.addEventListener('click', function(){
console.log(9);
});
const buttonZero = document.getElementById('number0');
buttonZero.addEventListener('click', function(){
console.log(0);
});
const buttonDecimal = document.getElementById('decimal');
buttonDecimal.addEventListener('click', function(){
console.log('.');
});
*{
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;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title> Calculator 8 </title>
<script src="fp.js" defer></script>
<link rel="stylesheet" href="fp.css">
</head>
<body>
<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>
</body>
</html>