pgb
06-30-2006, 10:36 AM
With this simple calculator + doesn't work, it puts numbers together instead of adding.
* / - all work fine
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Calculate</title>
</head>
<body>
<script type="text/javascript" language="javascript">
<!--
function checkform() {
var num1=document.addnumbers.num1.value; //get the first number from the form send it to a variable called num1
var num2=document.addnumbers.num2.value; //get the second number from the form send it to a variable called num2
function calculate (a,b) //create a function called calculate that will do a + b = x
{x = a + b; //this works fine with *, /, and -, but not with +, which it treats like a string!
return x;}
var total=calculate(num1,num2) //create a variable called total that will use the calculate function on num1 and num2
alert ("The total is " + total) //alert box will display total when form submitted
}
-->
</script>
<form onsubmit="return checkform()" name="addnumbers" action="" method="post">
Number 1<input type="text" name="num1"><br />
Number 2 <input type="text" name="num2"><br />
<input type="submit" class="button" value="Get the total!" />
</form>
</body>
</html>
* / - all work fine
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Calculate</title>
</head>
<body>
<script type="text/javascript" language="javascript">
<!--
function checkform() {
var num1=document.addnumbers.num1.value; //get the first number from the form send it to a variable called num1
var num2=document.addnumbers.num2.value; //get the second number from the form send it to a variable called num2
function calculate (a,b) //create a function called calculate that will do a + b = x
{x = a + b; //this works fine with *, /, and -, but not with +, which it treats like a string!
return x;}
var total=calculate(num1,num2) //create a variable called total that will use the calculate function on num1 and num2
alert ("The total is " + total) //alert box will display total when form submitted
}
-->
</script>
<form onsubmit="return checkform()" name="addnumbers" action="" method="post">
Number 1<input type="text" name="num1"><br />
Number 2 <input type="text" name="num2"><br />
<input type="submit" class="button" value="Get the total!" />
</form>
</body>
</html>