2

This is my HTML code:

<head>

</head>

<body>

   LIMIT<input id='limit' name='' value='' class=''>

   <button id='go' class=''>GO</button>

   TOTAL<input id='total' name='' value='' class=''>

   <script src='js/limitfor.js'></script>


</body>

And this is my JavaScript:

document.getElementById('go').onclick = function () {

 var limit = document.getElementById('limit').value;

 limit = parseFloat(limit);

 total = 0;

 for (i=0; i<=limit ;i++) {

     total = total + i;        

 };

};

If I alert the total, I can see that the function works, but I need the total to be in the textbox rather than in a pop up alert.

Phil
  • 157,677
  • 23
  • 242
  • 245
SereneAH
  • 23
  • 1
  • 3
  • possible duplicate of [set value of input using JS function](http://stackoverflow.com/questions/5700471/set-value-of-input-using-js-function) – Ivan Gerasimenko Feb 19 '15 at 05:12

4 Answers4

3

You will need to set the value of the input element:

document.getElementById("total").value = total;
Samuel Goodell
  • 600
  • 2
  • 9
2

First select the particular element (i.e. total text field) in the form and set its value using assignment operator '='

document.getElementById("total").value=total;
  • If you could please edit your answer and explain what the code you're showing does, and why/how that code answers the question, it could really help. – Lea Cohen Feb 19 '15 at 07:04
0

Just assign the value in total text box after your for loop is completed

 var limit = document.getElementById('limit').value;

 limit = parseFloat(limit);

 total = 0;

 for (i=0; i<=limit ;i++) {

   total = total + i;        

};
document.getElementById("total").value = total; 

};
Sandeeproop
  • 1,756
  • 1
  • 12
  • 18
0

use document.getElementById(put id of the text area where you want to output your answer or result).value = answer(whatever is your answer or result you want to reflect in textbox or textarea)

enter image description here