<div id="calc">
<div id="val"></div>
<div id="box-button">
<div class="btn" data-type="1">1</div>
<div class="btn" data-type="2">2</div>
<div class="btn" data-type="3">3</div>
<div class="btn" data-type="4">4</div>
<div class="btn" data-type="5">5</div>
<div class="btn" data-type="6">6</div>
<div class="btn" data-type="7">7</div>
<div class="btn" data-type="8">8</div>
<div class="btn" data-type="9">9</div>
<div class="btn" data-type="0">0</div>
<div class="btn" data-type=".">.</div>
<div class="btn" data-type="=">=</div>
</div>
<div class="znack">
<div class="btn" data-type="+">+</div>
<div class="btn" data-type="-">-</div>
<div class="btn" data-type="*">*</div>
<div class="btn" data-type="/">/</div>
<div class="btn" data-type="clear">clear</div>
</div>
</div>
#calc {
width: 190px;
margin: 100px auto;
text-align: center;
}
#box-button {
display: inline-block;
width: 140px;
vertical-align: top;
}
#txt {
width: 600px;
margin: 100px auto;
}
#val {
display: inline-block;
width: 100%;
height: 40px;
padding: 10px;
overflow: hidden;
line-height: 20px;
text-align: right;
border: 2px solid #ccc;
box-sizing: border-box;
}
.znack {
display: inline-block;
width: 42px;
}
.btn {
display: inline-block;
width: 40px;
height: 40px;
line-height: 40px;
border: 1px solid #ccc;
font-size: 14px;
text-align: center;
}
.btn:hover {
cursor: pointer;
color: #fff;
background-color: #f00;
}
window.onload = function(){
let itemSumm = document.getElementById('val');
let valOtvet = '';
let funClick = (event) => {
let valElementClick = event.target.getAttribute('data-type');
if(valElementClick === '='){
valOtvet = eval(valOtvet);
} else if(valElementClick === 'clear'){
valOtvet = '';
} else {
valOtvet += valElementClick;
}
itemSumm.innerHTML = valOtvet;
}
const items = document.getElementsByClassName('btn');
const btnLenght = items.length;
let i = 0;
for(;i<btnLenght;i++){
let item = items[i];
item.addEventListener('click',funClick);
}
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.