Calculated value and JavaScript: Creating a wall colour calculator

This article shows you how you can use JavaScript in combination with the ‘calculated value’ form element. This article uses a calculator for pricing paint to explain how you can calculate the amount of paint needed and the total cost, based on the area to be painted and the specific paint chosen. You will also learn the best ways to link various form values together and automate calculations.

Form creation, step by step

The cost calculator will be created on a new and empty form page that will only be used for this specific purpose. Follow the steps as given below:

Enter room dimensions

The room dimensions are needed for calculating the wall area to be painted.

Add a heading

  1. Add a heading: ‘Enter the room dimensions’. To do this, use the ‘paragraph’ form element.

Create the input fields

Create three input fields for the length, width and height of the room. Take care to ensure that the technical names are specified correctly, because these will be needed for the calculations.



Input field for length
Label Room length in metres
Technical name roomlength
Validation Number (smallest value: 0)

Input field for width
Label Room width in metres
Technical name roomwidth
Validation Number (smallest value: 0)

Input field for height
Label Room height in metres
Technical name roomheight
Validation Number (smallest value: 0)

Define areas to deduct

To ensure that windows and doors can be deducted from the overall area, these need to be specified in the form.

Input field for area to deduct

Label Area to deduct in square metres
Technical name areatodeduct
Validation Number (smallest value: 0)

Set up paint selection

You can let your users choose from different qualities of paint.

Add a heading

  1. Add the heading ‘Paint selection’.

Create a single choice field

Create a single choice field with various paint qualities.

Single choice field for paint quality

Label Required paint quality
Technical name paint
Options
  1. Label: Standard interior paint – EUR 15/l (value: 15)
  2. Label: Premium interior paint – EUR 25/l (value: 25)
  3. Label: Eco interior paint – EUR 35/l (value: 35)

Add code for JavaScript calculations

You can now automate the calculations with JavaScript.

Calculate wall area

Create a ‘calculated value’.


Calculated value for wall area

Label Wall area to be painted
Technischer Name wallarea
JavaScript
Check
  1. Show value in form
    With this option, the calculated value is visible directly in the form, so that users can immediately see the results of the calculation.
  2. Recalculate value in browser immediately

What the JavaScript function does:

  1. Calculates the base area: (length + width) × 2 × height
  2. Subtracts the area to deduct
  3. Uses parseInt(), which reads the input values as integers and ignores invalid characters

Calculate paint quantity

Create a ‘calculated value’.


Calculated value for paint quantity

Label Required paint quantity
Technical name paintquantity
JavaScript
Check
  1. Show value in form
  2. Recalculate value in browser immediately

What the JavaScript function does:

  1. Calculates the paint needed (0.125 litres per m²)
  2. Rounds up to whole litres

Calculate total cost

Create a ‘calculated value’.


Calculated value for total cost

Label Total cost
Technical name totalcost
JavaScript
Check
  1. Show value in form
  2. Recalculate value in browser immediately

The JavaScript function multiplies the calculated paint quantity with the litre price for the selected paint.

Summary

Your paint calculator is now ready to use and offers the following functions:

  1. Automatic calculation of wall areas
  2. Takes areas to deduct into account
  3. Determines the quantity of paint required
  4. Calculates the total cost

Test the calculator with various input values to confirm that all calculations are carried out correctly.

Paint Calculator template

This Paint Calculator is also available as a template from our website. The template can be found under Self Service .

The template has been designed only to perform a calculation – such as for a product on your website. The submit button is therefore hidden, because the form does not need to be submitted.

Feedback