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
4 Answers
Reset to default 3You 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
版权声明:本文标题:How do I return a value from a function in JavaScript to a textbox in HTML - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742232102a2437407.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论