admin管理员组文章数量:1302902
I am trying to make a simple html page with two text boxes and an a button that adds the two numbers together when clicked. In my output, I am only getting [object HTMLInputElement]
.
function addNumbers(A, B){
var answer = A + B;
document.getElementById("testResult").innerHTML = answer;
}
<input type="text" value="15" id="varA">
<input type="text" value="30" id="varB">
<input type="button" value="Add" onclick="addNumbers(varA, varB)">
<h1 id="testResult"></h1>
I am trying to make a simple html page with two text boxes and an a button that adds the two numbers together when clicked. In my output, I am only getting [object HTMLInputElement]
.
function addNumbers(A, B){
var answer = A + B;
document.getElementById("testResult").innerHTML = answer;
}
<input type="text" value="15" id="varA">
<input type="text" value="30" id="varB">
<input type="button" value="Add" onclick="addNumbers(varA, varB)">
<h1 id="testResult"></h1>
Any help would be appreciated. I tried changing .innerHTML
to .value
already but then I get nothing at all as a result.
-
4
varA
is theinput
element, not its value, which would bevarA.value
. However, you're going to need to change that to a number before adding it together, or else you'll end up with string concatenation. – user663031 Commented Oct 5, 2014 at 18:12 -
Note
input
elements have no content, so you can use<input ...>
or<input ... />
, but not<input ...></input>
. – Oriol Commented Oct 5, 2014 at 18:50
2 Answers
Reset to default 2I assume you want the mathematical sum and not the string concatenation. If that's the case, you can use the following:
UPDATE based on ment:
function addNumbers(elem1, elem2) {
var a = document.getElementById(elem1).value;
var b = document.getElementById(elem2).value;
var c = Number(a) + Number(b);
document.getElementById("testResult").innerHTML = c;
}
<input type="text" value="15" id="varA">
<input type="text" value="30" id="varB">
<input type="button" value="Add" onclick="addNumbers('varA', 'varB')"></input>
<h1 id="testResult"></h1>
Here's a working Fiddle: http://jsfiddle/JohnnyEstilles/ex09fx7k/.
Some fixes:
- You are adding up the inputs elements instead of their
value
. - To convert its string value to a number, you can use unary
+
. - Instead of inline event listeners, better use
addEventListener
.
var a = document.getElementById('varA'),
b = document.getElementById('varB'),
result = document.getElementById("testResult");
document.getElementById('add').addEventListener('click', function() {
addNumbers(a.value, b.value);
});
function addNumbers(n1, n2){
result.textContent = +n1 + +n2;
}
<input type="text" value="15" id="varA">
<input type="text" value="30" id="varB">
<input type="button" id="add" value="Add">
<h1 id="testResult"></h1>
本文标签: HTML javascript function issue object HTMLInputElement error outputStack Overflow
版权声明:本文标题:HTML javascript function issue. [object HTMLInputElement] error output - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741724042a2394534.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论