Sum
Page 1 of 1
Sum
Dear ExpertsI have following codes
- Code: Select all
<html>
<head>
<title>this is first math exercise within Java Script</title>
<script lanuage="Javascript">
function total()
{
Var amount1 = text1.value
Var amount2 = text2.value
Var total=eval(amount1) + eval(amount2)
text3.value=total
alter("Correct Answer")
}
</script>
</head>
<body>
<center>
<form>
<table width="20%" border="1" cellpadding="1" cellspacing="1" >
<tr>
<td width="10%" align="center">Amount1</td>
<td width="10%"><input type="number" name="text1" value="" min="1001" max="8000"></td>
</tr>
<tr>
<td width="10%" align="center">Amount2</td>
<td width="10%"><input type="number" name="text2" value="" min="1001" max="8000"></td>
</tr>
<tr>
<td width="10%" align="center">Total</td>
<td width="10%"><input type="number" name="text3" value="" min="1001" max="8000""></td>
</tr>
<tr>
<td width="10%"></td>
<td width="10%"><INPUT TYPE="button" NAME="Submit" VALUE="Compute" onclick="total()">
<input type=reset Value="Clear" size="30"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
I want to get summed value in text3 but function does not excecute.
What is wrong in my codes?
Please help
.
Re: Sum
There some thing i want to discuss with you, In your function what is alter, and some time you used capital letter for html tag and attributes, it's not a good practice. For the summing solution, you can use the code of the following,- Code: Select all
<html>
<head>
<title>this is first math exercise within Java Script</title>
<script type="text/javascript">
function addition(){
var amount1 = document.getElementById('text1').value;
var amount2 = document.getElementById('text2').value;
var total = eval(amount1) + eval(amount2);
var amount = document.getElementById('text3').value;
amount = eval(amount);
if(amount == total){
alert("Correct Answer")
}
else {
alert("Wrong Answer")
}
}
</script>
</head>
<body>
<center>
<form>
<table width="20%" border="1" cellpadding="1" cellspacing="1" >
<tr>
<td width="10%" align="center">Amount1</td>
<td width="10%"><input type="number" name="text1" id="text1" value="" min="1001" max="8000"></td>
</tr>
<tr>
<td width="10%" align="center">Amount2</td>
<td width="10%"><input type="number" name="text2" id="text2" value="" min="1001" max="8000"></td>
</tr>
<tr>
<td width="10%" align="center">Total</td>
<td width="10%"><input type="number" name="text3" id="text3" value="" min="1001" max="8000""></td>
</tr>
<tr>
<td width="10%"></td>
<td width="10%"><input type="button" name="Submit" value="Compute" onclick="addition()">
<input type=reset Value="Clear" size="30"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
Page 1 of 1