0

i tried this code in vs 2017 but it is not working. i cant console the total and other text variables but i am unable to populate the results in the text box.

function myFunction1() {

  var text1 = document.getElementById("txt1").value;
  console.log(text1)
  var text2 = document.getElementById("txt2").value;
  var text3 = document.getElementById("txt3").value;
  var text4 = document.getElementById("txt4").value;
  var total1 = parseFloat(text1) + parseFloat(text2) + parseFloat(text3) + parseFloat(text4);
  console.log(total1)

  document.getElementById("txt1").innerHTML = total1;
}
<input type="text" id="txt1" />
<input type="text" id="txt2" />
<input type="text" id="txt3" />
<input type="text" id="txt4" />
<input type="text" id="total" />

<input type="submit" onclick="myFunction1()" />
Serge K.
  • 5,303
  • 1
  • 20
  • 27
bali
  • 1

2 Answers2

1

Use value instead of innerHTML on your result input. Because input elements doesn't have innerHTML

document.getElementById("monthlyAmount").value = total1

innerHTML means HTML between starting and ending tag of element.As <input> elements doesn't have closing tag so it has no innerHTML

Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
0

You should use value to set the text in your input:

document.getElementById("monthlyAmount").value = total1;
Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63