Javascript calculator


Creating a small and simple calculator in Javascript is easy. It involves knowledge of basic HTML tables and using Form Name input properties. Personally I think its good practice to work with Javascript if you’re a beginner.

You are free to add other features such a scientific functions and various functions.
Also you are free to add your own styles to customise button colors or background design.

Sample code in Javascript

<FORM NAME="Calculator">
<TABLE BORDER=4 bgcolor="#0fffff">
<TR>
<TD>
<INPUT TYPE="text"   NAME="Input" Size="16">
<br>
</TD>
</TR>
<TR>
<TD>
<INPUT TYPE="button" NAME="one"   VALUE="  1  " OnClick="Calculator.Input.value += '1'">
<INPUT TYPE="button" NAME="two"   VALUE="  2  " OnCLick="Calculator.Input.value += '2'">
<INPUT TYPE="button" NAME="three" VALUE="  3  " OnClick="Calculator.Input.value += '3'">
<INPUT TYPE="button" NAME="add"  VALUE="  +  " OnClick="Calculator.Input.value += ' + '">
<br>
<INPUT TYPE="button" NAME="four"  VALUE="  4  " OnClick="Calculator.Input.value += '4'">
<INPUT TYPE="button" NAME="five"  VALUE="  5  " OnCLick="Calculator.Input.value += '5'">
<INPUT TYPE="button" NAME="six"   VALUE="  6  " OnClick="Calculator.Input.value += '6'">
<INPUT TYPE="button" NAME="subtract" VALUE="  -  " OnClick="Calculator.Input.value += ' - '">
<br>
<INPUT TYPE="button" NAME="seven" VALUE="  7  " OnClick="Calculator.Input.value += '7'">
<INPUT TYPE="button" NAME="eight" VALUE="  8  " OnCLick="Calculator.Input.value += '8'">
<INPUT TYPE="button" NAME="nine"  VALUE="  9  " OnClick="Calculator.Input.value += '9'">
<INPUT TYPE="button" NAME="multiply" VALUE="  x  " OnClick="Calulatorc.Input.value += ' * '">
<br>
<INPUT TYPE="button" NAME="clear" VALUE="  c  " OnClick="Calculator.Input.value = ''">
<INPUT TYPE="button" NAME="zero"  VALUE="  0  " OnClick="Calculator.Input.value += '0'">
<INPUT TYPE="button" NAME="Evaluate"  VALUE="  =  " OnClick="Calculator.Input.value = eval(Calculator.Input.value)">
<INPUT TYPE="button" NAME="divide"   VALUE="  /  " OnClick="Calculator.Input.value += ' / '">
<br>
</TD>
</TR>
</TABLE>
</FORM>

Advertisements

One thought on “Javascript calculator

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s