I am trying to make a simple calculator wher a person would in put two values and then when clicking a button an alert box would give the result. But it is not working.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
<!--//onclick handler //-->
function gonum()
{
var input1=document.getElementById('a').value;
var operator=document.getElementById('op').value;
var input2=document.getElementById('b').value;
if (operator=="+")
{
add(input1,input2);
}
}
function add(input1,input2)
{
alert(input1+input2);
}
</script>
</head>
<body>
<form>
<input type="text" name="a"/>
<input type="text" name="op"/>
<input type="text" name="b"/>
<input type="button" value="=" name="btn" onClick="gonum()" />
</form>
</body>
</html>
One glaring issue that I see is this:
Combined with this:
Observe that you aren’t assigning any IDs to the input elements; however, your JavaScript is attempting to call them by their (non-existent) id.
Change your html to this and you should be golden: