Can I pass a result of a calculation to Javascript percentage?

Asked

Viewed 669 times

0

The calculation I managed to do with Javascript, only that its output has to be in percentage.

<script>
var mb = document.querySelector("input[name=medidab]");
mb.addEventListener("keyup", calcImc, false);
function calcImc(){
   var md_val = parseFloat(mb.value);
   medidab = (md_val/50*100).toFixed(1);
   if(!isNaN(medidab)){
      document.querySelector("input[name=circunferenciabraco]").value = medidab;
   }
}
</script>

And here’s the form, I put the value in one field and the value of the calculation already comes out in the other. Only it has to come out in percentage and I don’t know if it’s possible.

<div>
   <input class="campo-form-c" type="text" name="medidab" placeholder="Medida do Braço" maxlength="5">
</div>
<div>
   <input class="campo-form-c" type="text" name="circunferenciabraco" placeholder="Circunferência do Braço" maxlength="5"></br>
</div>
  • 1

    Bruno, it wouldn’t be a case of just doing .value = medidab + '%';? You can explain better if it’s not that?

  • Hi Bruno, did you see the answer and comment here? That’s what you were looking for?

1 answer

1


As you have already managed to do the calculation, just concatenate the % in the result:

document.querySelector("input[name=circunferenciabraco]").value = medidab+"%";

var mb = document.querySelector("input[name=medidab]");
mb.addEventListener("keyup", calcImc, false);
function calcImc(){
   var md_val = parseFloat(mb.value);
   medidab = (md_val/50*100).toFixed(1);
   if(!isNaN(medidab)){
      document.querySelector("input[name=circunferenciabraco]").value = medidab+"%";
   }
}
<div>
   <input class="campo-form-c" type="text" name="medidab" placeholder="Medida do Braço" maxlength="5">
</div>
<div>
   <input class="campo-form-c" type="text" name="circunferenciabraco" placeholder="Circunferência do Braço" maxlength="5"></br>
</div>

Browser other questions tagged

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