Input field filling

Asked

Viewed 39 times

0

Good afternoon guys, it is the following, I have two input fields in a form, in the first input I enter a value, then I click to calculate and the second input is filled with value that is calculated by multiplying the received value in the first input by 2,40. Until then everything is fine, but I would like the second input to be filled in when the first one lost the focus without the need to click the calculate button. follows the code and the example

function calcularUFM(){
	var formulario = document.getElementById("formulario");
	
	var valor_ufm = +formulario.valor_ufm.value;
	
	var reais = (valor_ufm * 2.40);

	formulario.reais.value = reais.toFixed(2);
	
}
<html>
<head>
  <title></title>
  <link rel="stylesheet" href="css/estiloImc.css" type="text/css" media="all">
  <script type="text/javascript" src="js/scriptUfm.js"></script>
</head>

<body>
  <form id="formulario">
    <fieldset>
      <legend>CACULADORA UFM / REIAIS</legend>

      <label for="valor_ufm">VALOR EM UFM</label>
      <input type="text" name="valor_ufm" /><br /><br />

      <label for="reais"></label>
      <input type="text" name="reais" disabled="disabled" /><br /><br />

      <a href="#" onclick="calcularUFM();">calcular</a>
    </fieldset>
  </form>
</body>

</html>

1 answer

0


Can apply a onblur in the field calling the function:

formulario.valor_ufm.onblur = calcularUFM;

Or directly in the element:

<input type="text" name="valor_ufm" onblur="calcularUFM()" />
  • Thanks Sam, it worked , thanks! I used the second option

  • You are welcome. Learn how to thank in this link -> [tour]

Browser other questions tagged

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