0
I’m trying to make a sum of measures where each input has its specific value, eg: input 1 size 0.6 input 2 size 1.2 and so on, the idea is to take the value of the input recorded through the buttons + and - multiply by their size and then sum up all the total sizes recorded and present in the total measurement field.
At the point I am, after doing some isolated tests I believe that the function is maintaining the value of the inputs and multiplying by the added value.
I believe that first I will have to calculate and multiply individually each input or group of inputs that contain the same values and then add these totals, but I’m having a lot of trouble finding this logic in js, can someone give me a hand ? from now on thank.
function calc_elementos() {
const elements = document.querySelectorAll('.valor');
var soma = 0;
Array.from(elements).forEach((element, index) => {
soma += parseInt(element.value, 10);
});
return soma;
}
function calculo(num, valor) {
document.querySelector('.resultado').innerHTML = ((calc_elementos() + num) * valor).toFixed(2);
}
input[type="number"] {
-webkit-appearance: textfield;
-moz-appearance: textfield;
appearance: textfield;
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
}
.number-input {
border: 1px solid #ddd;
display: inline-flex;
}
.number-input button.plus {
background-color: #2eb82e;
}
.number-input button.down {
background-color: gainsboro;
}
.number-input,
.number-input * {
box-sizing: border-box;
}
.number-input button {
outline: none;
-webkit-appearance: none;
background-color: transparent;
border: none;
align-items: center;
justify-content: center;
width: 1rem;
height: 1rem;
cursor: pointer;
margin: 0;
position: relative;
}
.number-input button:before,
.number-input button:after {
display: inline-block;
position: absolute;
content: '';
width: 0.5rem;
height: 1px;
background-color: #212121;
transform: translate(-50%, -50%);
}
.number-input button.plus:after {
transform: translate(-50%, -50%) rotate(90deg);
}
.number-input input[type=number] {
font-family: sans-serif;
max-width: 2rem;
padding: .1rem;
border: solid #ddd;
border-width: 0 1.5px;
font-size: 1rem;
height: 1rem;
text-align: center;
}
h3 {
display: flexbox;
}
<main>
<section class="itens">
<div class="number-input">
<button onclick="this.parentNode.querySelector('input[type=number]').stepDown(calculo(-1, 1))" class="down"></button>
<input class="valor" min="0" name="valor1" value="0" type="number">
<button onclick="this.parentNode.querySelector('input[type=number]').stepUp(calculo(1, 1))" class="plus"></button>
</div>
<div class="number-input">
<button onclick="this.parentNode.querySelector('input[type=number]').stepDown(calculo(-1, 0.6))" class="down"></button>
<input class="valor" min="0" name="valor2" value="0" type="number">
<button onclick="this.parentNode.querySelector('input[type=number]').stepUp(calculo(1, 0.6))" class="plus"></button>
</div>
<div class="number-input">
<button onclick="this.parentNode.querySelector('input[type=number]').stepDown(calculo(-1, 1.1))" class="down"></button>
<input class="valor" min="0" name="valor3" value="0" type="number">
<button onclick="this.parentNode.querySelector('input[type=number]').stepUp(calculo(1, 1.1))" class="plus"></button>
</div>
<div class="number-input">
<button onclick="this.parentNode.querySelector('input[type=number]').stepDown(calculo(-1, 0.2))" class="down"></button>
<input class="valor" min="0" name="valor3" value="0" type="number">
<button onclick="this.parentNode.querySelector('input[type=number]').stepUp(calculo(1, 0.2))" class="plus"></button>
</div>
<div class="number-input">
<button onclick="this.parentNode.querySelector('input[type=number]').stepDown(calculo(-1, 0.5))" class="down"></button>
<input class="valor" min="0" name="valor3" value="0" type="number">
<button onclick="this.parentNode.querySelector('input[type=number]').stepUp(calculo(1, 0.5))" class="plus"></button>
</div>
</section>
<h3>Medida Total</h3>
<h3 class="resultado">0</h3>
</main>
The measure of buttons is the second parameter of the calculation function.
– paulo lima