Multiple results with pre-established value of one addition

Asked

Viewed 28 times

1

I have an input number and I would like when the user enters a value he makes an addition sum with a preset value by a variable and shows the result using Innerhtml (because I need modified value within a div:

Example:

        <input id="input-currency" required type="number" class="Input Input-currency Input--noSpinner" min="0.00" max="100000" step="500" value="10000" maxlength="7" data-number='{"toFixed":2,"stepfactor":10000}' />
        
        
  <div class="resultado_1"> somado com: 1.4% </div>
   <div class="resultado_2"> somado com: 2.5% </div>   
 <div class="resultado_3">  somado com: 1.3% </div>
  <div class="resultado_4">  somado com: 1.87% </div>
  
   <div class="resultado_5">  somado com: 1.6% </div>

1 answer

2

You can use the input change event (change), would be as follows:

var porcentagemUm = 1.4;
var porcentagemDois = 2.5;
var porcentagemTres = 1.3;
var porcentagemQuatro = 1.87;
var porcentagemCinco = 1.6;


document.querySelector('#input-currency').addEventListener('change',function(element){
    let value = parseInt(element.target.value);
    
    if(!!value) {
      document.querySelector('.resultado_1 b').innerHTML = (value + (value * (porcentagemUm / 100))).toFixed(2);

      document.querySelector('.resultado_2 b').innerHTML = (value + (value * (porcentagemDois / 100))).toFixed(2);

      document.querySelector('.resultado_3 b').innerHTML = (value + (value * (porcentagemTres / 100))).toFixed(2);

      document.querySelector('.resultado_4 b').innerHTML = (value + (value * (porcentagemQuatro / 100))).toFixed(2);

      document.querySelector('.resultado_5 b').innerHTML = (value + (value * (porcentagemCinco / 100))).toFixed(2);
    } else {
      document.querySelector('.resultado_1 b').innerHTML = '';
      document.querySelector('.resultado_2 b').innerHTML = '';
      document.querySelector('.resultado_3 b').innerHTML = '';
      document.querySelector('.resultado_4 b').innerHTML = '';
      document.querySelector('.resultado_5 b').innerHTML = '';
    }
    
    
    
});
<input id="input-currency" required type="number" class="Input Input-currency Input--noSpinner" min="0.00" max="100000" step="500" value="" maxlength="7" data-number='{"toFixed":2,"stepfactor":10000}' />
        
        
<div class="resultado_1">somado com: 1.4% <b></b> </div>
<div class="resultado_2">somado com: 2.5% <b></b> </div>   
<div class="resultado_3">somado com: 1.3% <b></b> </div>
<div class="resultado_4">somado com: 1.87% <b></b> </div>
<div class="resultado_5">somado com: 1.6% <b></b> </div>

  • Exactly that Vinicius! Thank you very much! Just one more thing, for me to leave an initial value in Input example: 10,000, I have to create a variable and assign that value to the input?

  • @Ogaiteuqireh you can make this calculation into a function that takes the input value as parameter, and as soon as js loads you call this function passing the input initial value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.