
Originally Posted by
tomwaits4noman
I am trying to create a basic javascript calculator
the problem is that I can code it to add and minus or multiple and divide but not all four functions using the code I have below.
thanks
<html>
<head>
<title>Template</title>
<script>
function add(numID1, numID2)
{
var myString = "";
var num1 = document.getElementById(numID1).value;
num1 *= 1;
var num2 = document.getElementById(numID2).value;
num2 *= 1;
op = document.getElementById('opChoice').value;
if(op == 'divide')
ans = num1 / num2;
else
ans = num1 * num2;
myString = ans;
document.getElementById('content').innerHTML = myString;
}
function pickOp(val)
{
if(val.length > 0)
document.getElementById('opChoice').value = val;
}
</script>
<link rel="stylesheet" href="style/layout.css" media="screen" />
</head>
<body>
<div id="container">
<div id="header">
<br />
Header info goes here
</div>
<div id="submenu">
</div>
<div id="navmenu">
<input type="text" id="num1" value="0" /><br />
<select onChange="pickOp(this.value);">
<option value="" />
<option value="divide" />Divide
<option value="multiple" />Multiple
</select>
<input type="text" id="opChoice" value="divide" /><br />
<input type="text" id="num2" value="0" /><br />
<button onClick="add('num1', 'num2');">Equals</button><br />
</div>
<div id="content">
Content goes here
</div>
</div>
</body>
</html>
innerHTML is of the devil. :|
document.write is document.wrong
Bookmarks