Javascripts > Misc Javascripts > Simple Calculator
Script Title: Simple Calculator
Description: A simple calculator.
Example: Available
Copy the following code into your <HEAD></HEAD> tags.<script language="JavaScript"> var m; m = 0; var i; var mm; function num (i) { var u = new String(); u = text3.value; if (u == "=") { text1.value = text2.value = ""; text1.value = text1.value + i ; text3.value = ""; return; } var s; var l; s = text1.value; l = s.length; if (l<10) { text1.value = text1.value + i; } } function point() { var i; var s = new String(); s = text1.value; i = s.indexOf("."); var u = new String(); u = text3.value; if (u == "=") { text1.value = text2.value = ""; text1.value = text1.value + "." ; text3.value = ""; return; } if (i > -1) { return; } else { if (s.length<9) { s = s + "."; text1.value = s; } } } var cho; function act(cho) { var u = new String(); u = text3.value; if (u == "") { text2.value = text1.value; text1.value = ""; } else { switch (u) { case "+" : text2.value = (text2.value*1) + (text1.value*1); text1.value = ""; break; case "-" : text2.value = (text2.value*1) - (text1.value*1); text1.value = ""; break; case "*" : text2.value = (text2.value*1) * (text1.value*1); text1.value = ""; break; case "/" : text2.value = (text2.value*1)/(text1.value*1); text1.value = ""; break; case "=" : text2.value = text1.value; text1.value = ""; break; } } switch (cho) { case 1 : text3.value = "+"; break; case 2 : text3.value = "-"; break; case 3 : text3.value = "*"; break; case 4 : text3.value = "/"; break; } } function eq() { switch (text3.value) { case "+": text2.value = (text2.value*1) + (text1.value*1); text1.value = text2.value; text2.value = ""; break; case "-": text2.value = (text2.value*1) - (text1.value*1); text1.value = text2.value; text2.value = ""; break; case "*": text2.value = (text2.value*1) * (text1.value*1); text1.value = text2.value; text2.value = ""; break; case "/": text2.value = (text2.value*1) / (text1.value*1); text1.value = text2.value; text2.value = ""; break; } text3.value = "="; } function clss() { text1.value = text2.value = text3.value = ""; m =0; clear1.blur(); } function initia() { text1.blur(); text2.blur(); } function clearall() { text1.value = ""; } function madd() { m = m + ((text1.value)*1); text1.value = ""; text3.value = ""; } function msub() { m = m - ((text1.value)*1); text1.value = ""; text3.value = ""; } function mrem() { if (m != 0) { text1.value = m; text3.value = ""; } } function free() { if (mm != 1) { alert ('JavaScript disables this action!!'); } } function frees() { mm = 1; } function freed() { mm = 0; } </script>
Copy the following code between the <BODY></BODY> tags of your html page.<pre> <table bgcolor=green border=0> <tr bgcolor=green><input type=text name="text2" style="background-color:black;color:lime;width:150" onFocus="initia()"><input type=hidden name="text3" style="background-color:black;color:lime;width:25"></tr> <tr bgcolor=green><input type=text name="text1" style="background-color:black;color:lime;width:150" onFocus="initia()"></tr> <tr><td><input type=button value="1" style="background-color:black;color:lime;width=25" onMouseOver="frees()" onMouseOut="freed()" onClick="num(1)"></td><td><input type=button value="2" style="background-color:black;color:lime;width=25" onMouseOver="frees()" onMouseOut="freed()" onClick="num(2)"></td><td><input type=button value="3" style="background-color:black;color:lime;width=25" onMouseOver="frees()" onMouseOut="freed()" onClick="num(3)"></td><td><input type=button value="+" style="background-color:black;width=25;color:lime" onMouseOver="frees()" onMouseOut="freed()" onClick="act(1)"></td><td><input type=button value="CE" style="background-color:black;color:lime;width=25" onMouseOver="frees()" onMouseOut="freed()" onClick="clearall()"></td></tr> <tr><td><input type=button value="4" style="background-color:black;width=25;color:lime" onMouseOver="frees()" onMouseOut="freed()" onClick="num(4)"></td><td><input type=button value="5" style="background-color:black;width=25;color:lime" onMouseOver="frees()" onMouseOut="freed()" onClick="num(5)"></td><td><input type=button value="6" style="background-color:black;width=25;color:lime" onMouseOver="frees()" onMouseOut="freed()" onClick="num(6)"></td><td><input type=button value="-" style="background-color:black;color:lime;width=25" onMouseOver="frees()" onMouseOut="freed()" onClick="act(2)"></td><td><input type=button value="M+" style="background-color:black;color:lime;width=25" onMouseOver="frees()" onMouseOut="freed()" onClick="madd()"></td></tr> <tr><td><input type=button value="7" style="background-color:black;width=25;color:lime" onMouseOver="frees()" onMouseOut="freed()" onClick="num(7)"></td><td><input type=button value="8" style="background-color:black;width=25;color:lime" onMouseOver="frees()" onMouseOut="freed()" onClick="num(8)"></td><td><input type=button value="9" style="background-color:black;width=25;color:lime" onMouseOver="frees()" onMouseOut="freed()" onClick="num(9)"></td><td><input type=button value="*" style="background-color:black;width=25;color:lime" onMouseOver="frees()" onMouseOut="freed()" onClick="act(3)"></td><td><input type=button value="M-" style="background-color:black;color:lime;width=25" onMouseOver="frees()" onMouseOut="freed()" onClick="msub()"></td></tr> <tr><td><input type=button value="." style="background-color:black;width=25;color:lime" onMouseOver="frees()" onMouseOut="freed()" onClick="point()"></td><td><input type=button value="0" style="background-color:black;width=25;color:lime" onMouseOver="frees()" onMouseOut="freed()" onClick="num(0)"></td><td><input type=button value="=" style="background-color:black;color:lime;width=25" onMouseOver="frees()" onMouseOut="freed()" onClick="eq()"></td><td><input type=button value="/" style="background-color:black;width=25;color:lime" onMouseOver="frees()" onMouseOut="freed()" onClick="act(4)"></td><td><input type=button value="M" style="background-color:black;color:lime;width=25" onMouseOver="frees()" onMouseOut="freed()" onClick="mrem()"></td></tr> <tr bgcolor=green><input type=button name="clear1" value="Clear All" style="background-color:black;color:lime;width=150;height=20" onMouseOver="frees()" onMouseOut="freed()" onClick="clss()"></tr> </table></pre>
Enter your name and email address to have this script sent right to your Inbox for later viewing. First Name Last Name Email