How to Create a Simple Online Calculator
HTML Code
<br/><br/><br/><br/> <label id="calculation"></label> <br/> <table style="text-align: left; width: 265px; height: 118px;" border="0" cellpadding="2" cellspacing="2"> <tr> <td colspan="4" rowspan="1" style="vertical-align: top;"> <div id='mainarea' contenteditable style="width:100%; height:20px; border: 1px solid black;"></div> </td> </tr> <tr> <td style="vertical-align: top; text-align: center;" > <input TYPE="button" NAME="7" VALUE=" 7 " onclick="updateTextbox(7)"> <input TYPE="button" NAME="8" VALUE=" 8 " onclick="updateTextbox(8)"> <input TYPE="button" NAME="9" VALUE=" 9 " onclick="updateTextbox(9)"> <input TYPE="button" NAME="+" VALUE=" + " onclick="updateTextbox('+')"> <br> <input TYPE="button" NAME="4" VALUE=" 4 " onclick="updateTextbox(4)"> <input TYPE="button" NAME="5" VALUE=" 5 " onclick="updateTextbox(5)"> <input TYPE="button" NAME="6" VALUE=" 6 " onclick="updateTextbox(6)"> <input TYPE="button" NAME="-" VALUE=" - " onclick="updateTextbox('-')"> <br> <input TYPE="button" NAME="1" VALUE=" 1 " onclick="updateTextbox(1)"> <input TYPE="button" NAME="2" VALUE=" 2 " onclick="updateTextbox(2)"> <input TYPE="button" NAME="3" VALUE=" 3 " onclick="updateTextbox(3)"> <input TYPE="button" NAME="*" VALUE=" x " onclick="updateTextbox('*')"> <br> <input TYPE="button" NAME="c" VALUE=" c " onclick="reset();"> <input TYPE="button" NAME="0" VALUE=" 0 " onclick="updateTextbox(0)"> <input TYPE="button" NAME="=" VALUE=" = " onclick="result();"> <input TYPE="button" NAME="/" VALUE=" / " onclick="updateTextbox('/')"> </td> </tr> </table>
Javascript Code
<script type = "text/javascript" > function updateTextbox(value){ document.getElementById("screen").value += value; } function result(){ document.getElementById("screen").value = eval(document.getElementById("screen").value); } function reset(){ document.getElementById("screen").value = ''; </script>