name _F1
09-03-2006, 01:27 PM
I made myself a basic Windows-style calculator using Javascript and HTML forms. The code is as follows:
<form name="calc" id="calc" action="javascript:calc.output.value = eval(calc.output.value);">
<input type="text" name="output" id="output" value="" />
<table>
<tr>
<td class="calctd"><input type="button" class="calcbtn" value="7" onclick="calc.output.value += '7'" />
<td class="calctd"><input type="button" class="calcbtn" value="8" onclick="calc.output.value += '8'" />
<td class="calctd"><input type="button" class="calcbtn" value="9" onclick="calc.output.value += '9'" />
<td class="calctd"><input type="button" class="calcbtn" value="/" onclick="calc.output.value += '/'" />
</tr>
<tr>
<td class="calctd"><input type="button" class="calcbtn" value="4" onclick="calc.output.value += '4'" />
<td class="calctd"><input type="button" class="calcbtn" value="5" onclick="calc.output.value += '5'" />
<td class="calctd"><input type="button" class="calcbtn" value="6" onclick="calc.output.value += '6'" />
<td class="calctd"><input type="button" class="calcbtn" value="*" onclick="calc.output.value += '*'" />
</tr>
<tr>
<td class="calctd"><input type="button" class="calcbtn" value="1" onclick="calc.output.value += '1'" />
<td class="calctd"><input type="button" class="calcbtn" value="2" onclick="calc.output.value += '2'" />
<td class="calctd"><input type="button" class="calcbtn" value="3" onclick="calc.output.value += '3'" />
<td class="calctd"><input type="button" class="calcbtn" value="-" onclick="calc.output.value += '-'" />
</tr>
<tr>
<td class="calctd"><input type="button" class="calcbtn" value="0" onclick="calc.output.value += '0'" />
<td class="calctd"><input type="button" class="calcbtn" value="." onclick="calc.output.value += '.'" />
<td class="calctd"><input type="button" class="calcbtn" value="=" onclick="calc.output.value = eval(calc.output.value)" />
<td class="calctd"><input type="button" class="calcbtn" value="+" onclick="calc.output.value += '+'" />
</tr>
</table>
</form>
It works fine. However, I want the answer to be evaluated when the Enter key is pressed, also. I tried adding the Javascript in the form's action attribute, but when I submit the form it goes to a new page showing the answer [and only the answer] rather than just changing the value of the input field.
If possible, I'd like to keep any method to the action/onsubmit attributes rather than adding a function to check if the Enter key is pressed. However, if this can't be done then a function when the Enter key is pressed will be fine.
Any help would be appreciated. :o
EDIT: I found another error, in IE. If the syntax for the Maths is not correct, it causes an error. Is there any way to either stop multiple operators/decimal points immediately next to each other, or simply stop the errors?
<form name="calc" id="calc" action="javascript:calc.output.value = eval(calc.output.value);">
<input type="text" name="output" id="output" value="" />
<table>
<tr>
<td class="calctd"><input type="button" class="calcbtn" value="7" onclick="calc.output.value += '7'" />
<td class="calctd"><input type="button" class="calcbtn" value="8" onclick="calc.output.value += '8'" />
<td class="calctd"><input type="button" class="calcbtn" value="9" onclick="calc.output.value += '9'" />
<td class="calctd"><input type="button" class="calcbtn" value="/" onclick="calc.output.value += '/'" />
</tr>
<tr>
<td class="calctd"><input type="button" class="calcbtn" value="4" onclick="calc.output.value += '4'" />
<td class="calctd"><input type="button" class="calcbtn" value="5" onclick="calc.output.value += '5'" />
<td class="calctd"><input type="button" class="calcbtn" value="6" onclick="calc.output.value += '6'" />
<td class="calctd"><input type="button" class="calcbtn" value="*" onclick="calc.output.value += '*'" />
</tr>
<tr>
<td class="calctd"><input type="button" class="calcbtn" value="1" onclick="calc.output.value += '1'" />
<td class="calctd"><input type="button" class="calcbtn" value="2" onclick="calc.output.value += '2'" />
<td class="calctd"><input type="button" class="calcbtn" value="3" onclick="calc.output.value += '3'" />
<td class="calctd"><input type="button" class="calcbtn" value="-" onclick="calc.output.value += '-'" />
</tr>
<tr>
<td class="calctd"><input type="button" class="calcbtn" value="0" onclick="calc.output.value += '0'" />
<td class="calctd"><input type="button" class="calcbtn" value="." onclick="calc.output.value += '.'" />
<td class="calctd"><input type="button" class="calcbtn" value="=" onclick="calc.output.value = eval(calc.output.value)" />
<td class="calctd"><input type="button" class="calcbtn" value="+" onclick="calc.output.value += '+'" />
</tr>
</table>
</form>
It works fine. However, I want the answer to be evaluated when the Enter key is pressed, also. I tried adding the Javascript in the form's action attribute, but when I submit the form it goes to a new page showing the answer [and only the answer] rather than just changing the value of the input field.
If possible, I'd like to keep any method to the action/onsubmit attributes rather than adding a function to check if the Enter key is pressed. However, if this can't be done then a function when the Enter key is pressed will be fine.
Any help would be appreciated. :o
EDIT: I found another error, in IE. If the syntax for the Maths is not correct, it causes an error. Is there any way to either stop multiple operators/decimal points immediately next to each other, or simply stop the errors?