Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  147] [ 1]  / answers: 1 / hits: 115967  / 12 Years ago, thu, june 14, 2012, 12:00:00

I have a simple HTML form, with 3 input field, when you hit = button, it suppose to add 2 numbers that you typed in the first 2 input fields and display result in 3rd filed. Is it possible to do that? (I need result in a box (or table) or some thing rather than in plain text). I try the following but doesn't work (it does nothing), can somebody help me?



<HTML>
<HEAD>
<TITLE>Sum</TITLE>

<script type=text/javascript>
function sum()
{
var num1 = document.myform.number1.value;
var num2 = document.myform.number2.value;
var sum = parseInt(num1) + parseInt(num2);
document.getElementById('add').innerHTML = sum;
}
</script>
</HEAD>

<BODY>
<FORM NAME=myform>
<INPUT TYPE=text NAME=number1 VALUE=> +
<INPUT TYPE=text NAME=number2 VALUE=>
<INPUT TYPE=button NAME=button Value== onClick=sum()>
<INPUT TYPE=text ID=add NAME=result VALUE=>
</FORM>

</BODY>
</HTML>

More From » html

 Answers
14

innerHTML sets the text (including html elements) inside an element. Normally we use it for elements like div, span etc to insert other html elements inside it.



For your case you want to set the value of an input element. So you should use the value attribute.



Change innerHTML to value



document.getElementById('add').value = sum;

[#84891] Thursday, June 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
keric

Total Points: 572
Total Questions: 93
Total Answers: 97

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
;