admin管理员组

文章数量:1327750

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.

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.

Share edited Feb 19, 2015 at 5:10 Phil 165k25 gold badges262 silver badges267 bronze badges asked Feb 19, 2015 at 5:08 SereneAHSereneAH 231 silver badge3 bronze badges 1
  • possible duplicate of set value of input using JS function – Ivan Gerasimenko Commented Feb 19, 2015 at 5:12
Add a ment  | 

4 Answers 4

Reset to default 3

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

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

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;

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

 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; 

};

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)

本文标签: How do I return a value from a function in JavaScript to a textbox in HTMLStack Overflow