Print a Javascript variable in Input html

0
  <script>
    function Sumar() {
        var n7 = document.getElementById('txtN7').value; /*Fator tempo*/
        var n8 = n7.replace(',','.');   
        var cem = (n8)/4; /*Calculo de fator*/
        var n1 = document.getElementById('txtN1').value; /*100%*/
        var n2 = document.getElementById('txtN2').value; /*101%*/

        var suma = ((parseInt(n1) + parseInt(n2))*cem); /*Resultado somenta dos valores de 100 e 101*/

        var n3 = document.getElementById('txtN3').value; /*95-99%*/
        var n4 = document.getElementById('txtN4').value; /*85%-94%*/
        var n5 = document.getElementById('txtN5').value; /*75%-84*/
        var n6 = document.getElementById('txtN6').value; /*50%-74%*/
        var n9 = document.getElementById('txtN9').value; /*No Match*/

        var suma1 = (((parseInt(n3) + parseInt(n4)+ parseInt(n5)+parseInt(n6)+parseInt(n9))*n8)+suma);
        document.write(suma1);
    /* alert("Número estimado em horas: "+suma1) */
       }
</script>

Good afternoon everyone !!! I need to create an "Input Text" of Html5 to print the variable suma1 of javascript. I used the document.write more by clicking the button it makes a refresh type and shows the value on a blank page. Thanks in advance!

<input type="button" onclick="Sumar();" value="Calcular Horas Tradução">
    
asked by anonymous 26.02.2018 / 19:35

2 answers

0

Your problem is that you are using document.write, this command will replace all the content on your page. You should create a div to "print" the result, see the example below:

function Sumar() {
  var n7 = document.getElementById('txtN7').value; /*Fator tempo*/
  var n8 = n7.replace(',','.');   
  var cem = (n8)/4; /*Calculo de fator*/
  var n1 = document.getElementById('txtN1').value; /*100%*/
  var n2 = document.getElementById('txtN2').value; /*101%*/

  var suma = ((parseInt(n1) + parseInt(n2))*cem); /*Resultado somenta dos valores de 100 e 101*/

  var n3 = document.getElementById('txtN3').value; /*95-99%*/
  var n4 = document.getElementById('txtN4').value; /*85%-94%*/
  var n5 = document.getElementById('txtN5').value; /*75%-84*/
  var n6 = document.getElementById('txtN6').value; /*50%-74%*/
  var n9 = document.getElementById('txtN9').value; /*No Match*/

  var suma1 = (((parseInt(n3) + parseInt(n4)+ parseInt(n5)+parseInt(n6)+parseInt(n9))*n8)+suma);

  document.getElementById("resultado").innerHTML = suma1;
  /* alert("Número estimado em horas: "+suma1) */
}

The document.write command is replaced by document.getElementById ("result"). innerHTML = suma1;

And you should create a div with this id as below

<input type="button" onclick="Sumar();" value="Calcular Horas Tradução">
<div id="resultado"></div>
    
26.02.2018 / 20:11
0

Use document.getElementById('id-do-campo').value = suma1;

In this way the value of the suma1 variable will be added in the value attribute of input

To create dynamically, just use document.createElement("input") , then just assign the value in the value property

Example:

function Sumar(element) {
  var n7 = document.getElementById('txtN7').value; /*Fator tempo*/
  var n8 = n7.replace(',', '.');
  var cem = (n8) / 4; /*Calculo de fator*/
  var n1 = document.getElementById('txtN1').value; /*100%*/
  var n2 = document.getElementById('txtN2').value; /*101%*/

  var suma = ((parseInt(n1) + parseInt(n2)) * cem); /*Resultado somenta dos valores de 100 e 101*/

  var n3 = document.getElementById('txtN3').value; /*95-99%*/
  var n4 = document.getElementById('txtN4').value; /*85%-94%*/
  var n5 = document.getElementById('txtN5').value; /*75%-84*/
  var n6 = document.getElementById('txtN6').value; /*50%-74%*/
  var n9 = document.getElementById('txtN9').value; /*No Match*/

  var suma1 = (((parseInt(n3) + parseInt(n4) + parseInt(n5) + parseInt(n6) + parseInt(n9)) * n8) + suma);
  

  /* Cria input e adiciona o valor da variável */
  let input = document.createElement("input");
  input.value = suma1;
  
  /* Inclui o input no elemento body */
  document.body.appendChild( input )
}
<input type="hidden" id="txtN1" value="1" />
<input type="hidden" id="txtN2" value="1" />
<input type="hidden" id="txtN3" value="1" />
<input type="hidden" id="txtN4" value="1" />
<input type="hidden" id="txtN5" value="1" />
<input type="hidden" id="txtN6" value="1" />
<input type="hidden" id="txtN7" value="1" />
<input type="hidden" id="txtN8" value="1" />
<input type="hidden" id="txtN9" value="1" />

<input type="button" onclick="Sumar();" value="Calcular Horas Tradução">

The document.write will erase the contents of the screen and replace with the indicated value.

    
26.02.2018 / 20:03