Javatpoint Logo
Javatpoint Logo

Simple Calculator in HTML using eval() in JavaScript and CSS

Building a calculator is one of the first challenges to take when learning HTML and JavaScript. This tutorial covers the simplest method that can be used to create a simple calculator using HTML, CSS and JavaScript. We'll create a simple webpage with a calculator.

Simple Introduction to HTML, CSS and JS:

  1. HTML is the markup language used to structure a webpage.
  2. CSS is the styling language used to design the layout and look of the elements of the webpage.
  3. JS is the scripting language used to define the behavior and working of the elements in the webpage.

eval () method:

The function eval() accepts an expression in the form of a string as input, evaluates it and returns the result.

For example:

eval("4*3+2") returns 14

However, using eval() is not recommended as it can lead to malicious attacks. As discussed above eval() can accept an expression in any form even large strings. If someone-a hacker or some mischievous user gives it an infinite value, it keeps executing slowing down the whole server trying to evaluate the expression rather than identifying the attack.

  • Using eval () can be a security hazard. If the program accepts a user input and passes it to eval(), a hacker can inject a malicious code as input that runs arbitrarily in the scope of the function leaking confidential credentials like login details, etc.
  • We need to sanitize the input of the eval () function or it is better to use some other function in the place of eval ().

For a beginner level calculator design, we'll use it for now.

Idea:

The idea is to create a table. The first row of the table should be the display. The next rows must contain buttons of numbers, clear, Back space, equals to and operators.

  1. On clicking numbers or operators, the value of the button must be displayed on the display.
  2. On clicking on C, the display has to become empty.
  3. On clicking B, the last element on the display must be deleted.
  4. On clicking =, everything on the display must be solved using eval() and now the display has to show the result of the expression entered.

Hence, we need to write 4 functions in JavaScript for the functionalities of four types of buttons. You can style the calculator anyway you want using CSS. For simplicity, we aren't using many styling features.

We'll make a calculator like this:

Simple Calculator in HTML using eval() in JavaScript and CSS
  • We used io to develop the code. You can use a text editor or any software you want.

The HTML part:

  1. Create a new file with .html extension: calci.html
  2. In the head, we've given the title as calci. The first tag in body is center, to place the heading and the calculator in the middle of the web page.
  3. Then, we gave the main heading of the web page as "Simple Calculator".
  4. We constructed a table, such that:
  5. Every row consists of buttons to be pressed and on every button click, a function from js is called for the calculation.
  6. Buttons in the first row: 0, (, ), empty column, C, B. C is for clearing the display and B is for giving a back space.
  7. Buttons in the second row: 7, 8, 9, empty column, +, and -.
  8. Buttons in the third row: 6, 5, 4, empty column, *, and /.
  9. Buttons in the fourth row: 1, 2, 3, empty column, decimal point and =.
  10. We'll be using inline CSS and js in the head section with <style></style> and <script></script> tags.

Here is the HTML code:

Output:

Simple Calculator in HTML using eval() in JavaScript and CSS
  • On pressing these buttons, nothing happens.
  • Before going to the styling part, we'll work on the functionality part:

JavaScript Part:

  1. We've already discussed about the eval() function. After taking the input from the user, we'll give the expression as input to the function.
  2. Observe the HTML code, we've called four functions fun(), C(), B() and res().
  3. fun(a):
    1. On clicking all the buttons of numbers and operators, we call fun () with the value of the number or operator as argument.
    2. In the function, we need to print that value on the display input box, hence, we concatenate the argument to the value already in the input box.
  4. C(): We'll simply reassign the value of the display input box to an empty string to clear the display.
  5. B(): We'll get the whole string on the display and slice it deleting the last element and re-assign it to the display again.
  6. res(): This function is activated on clicking =. We'll just get the whole expression on the display and pass it as an argument to eval() and reassign the result to the display.

Inline JS Code:

Output:

Simple Calculator in HTML using eval() in JavaScript and CSS

Now, it is time to design the web page using CSS:

CSS Part:

Here is a table of all the CSS properties used with explanation:

Table styling:

Property Explanation
table_layout: fixed The layout of all the cells in the table remains fixed (Width of the columns) irrespective of the length of content inside.
border-spacing: 10px Distance or space between the borders of cells. It applies only when border-collapse is set to separate. We can set both vertical and horizontal distances.
box-shadow To attach a shadow to an element. We can add multiple shadows to a single element by separating them by a comma.
border-radius To curve the edge of the element's corners. (To add rounded corners to an element). The greater the value, rounder the corner will be. We can give four values to four corners.
background-color The color to the background of an element. We can give the name of the color or use hexadecimal codes.
border One property for adding the width, style and color to the borders of an element.

Button:

Property Explanation
padding To create space between the element and the border around it. Using padding-top, padding-right, padding-bottom and padding-left, we can give spaces on each side of the element.
border-radius To curve the edge of the element's corners. (To add rounded corners to an element). The greater the value, rounder the corner will be. We can give four values to four corners.
hover: background-color Using element:hover, we can add a property to the element when user hovers (moves) the cursor on the element. Here, we are changing the background color.
color Font color.
active: tranform Using element:active, we can add a property to the element when user clicks on the element. Transform is used to add 2D effects to the elements. Here, we are using scale(). This function can resize the element and then back to the original size. We used this to get a button effect when pressed.
body: hover h1{} When user hovers on the body, we can change the property of h1 using this syntax.

Final result:







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA