Weight Converter Application using Pure JavaScript Tutorial

In this tutorial, you will learn how to create a simple Weight Converter Application. This tutorial aims to provide students and beginners with a reference for learning some techniques for building a simple web application using Pure JavaScript. Here, I will be providing a simple web page script that demonstrates the creation of a Weight Converter App. A complete source code zip file is also provided and is free to download.

What is a Weight Converter Application?

Here, I am referring to a simple web application that converts the weight value from a mass unit into another unit. The weight converter application allows the user to enter a certain mass value such as kilograms (kg.), grams (g), etc, and automatically converts the value to another mass value. For example, if the user would like to convert a value with a kilogram unit into pounds, the user can simply enter the value into the kilogram provided input, and the system or application automatically computes or convert it into the pound value.

How to Create a Weight Converter Application?

The Weight Converter Application can be achieved easily using the built-in methods/functions and event listener of JavaScript. The most important for creating this type of web application is to know or identify the correct formula for converting a mass unit to another. Below is the formula for mass conversion that we'll be using in this tutorial.

  • Kilogram (kg) to Pounds (lbs) - kg * 2.2
  • Kilogram (kg) to Grams (g) - kg * 1000
  • Grams (kg) to Kilogram (g) - g / 1000
  • Grams (kg) to Pounds (g) - (g / 1000) * 2.2
  • Pounds (kg) to Kilogram (g) - lbs / 2.2
  • Pounds (kg) to Grams (g) - (lbs / 2.2 ) * 1000

Then using the JS event listeners such as the input event can help us to trigger the conversion while the user inputs or enter values to the specific input field. The input field listens to the input element and executes the added JS script function to when users update the value or add/remove a character to the input field. Lastly, create the conversion function in JavaScript using the given formula. Check out the sample web page scripts that I created and provided below to have a better idea of how to apply this to an actual web application.

Sample Web Page Scripts

The following scripts result in a simple web application page that contains 3 input fields which are the kilogram, gram, and pound input fields. When the user puts or enters a value on a certain mass unit or input field, the application will automatically compute the value conversions of the other mass unit input fields.

HTML

Here is the HTML file script known as index.html. The file contains the HTML elements of the page layout and form input fields.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Weight Converter App</title>
  6. <link rel="stylesheet" href="style.css">
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. </head>
  10. <div class="container">
  11. <h1 id="page-title">Weight Converter App using JavaScript</h1>
  12. <hr id="title_hr">
  13. <!-- Form Wrapper -->
  14. <div id="form-wrapper">
  15. <div class="input-container">
  16. <label for="kg">Kilogram(s)</label>
  17. <input type="number" step="any" id="kg" value="0">
  18. </div>
  19. <div class="input-container">
  20. <label for="g">Gram(s)</label>
  21. <input type="number" step="any" id="g" value="0">
  22. </div>
  23. <div class="input-container">
  24. <label for="lbs">Pound(s)</label>
  25. <input type="number" step="any" id="lbs" value="0">
  26. </div>
  27. </div>
  28. <!-- Form Wrapper -->
  29. <script src="script.js"></script>
  30. </body>
  31. </html>

CSS

Next, here's the CSS file script known as style.css. The file contains the stylesheet scripts of some of the page layouts and other elements of custom design.

  1. @import url('https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;1,100;1,200;1,300;1,400;1,500;1,600&display=swap');
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. font-family: 'Roboto Mono', monospace;
  7. }
  8. ::selection{
  9. color: #fff;
  10. background: #4db2ec;
  11. }
  12. body{
  13. display: flex;
  14. align-items: center;
  15. justify-content: center;
  16. min-height: 100vh;
  17. background: #0B2447;
  18. background-image: linear-gradient(to right, #2E4F4F 0%, #0B2447 100%);
  19. }
  20. #page-title{
  21. color: #fff;
  22. text-align: center;
  23. font-weight: 500;
  24. }
  25. #title_hr{
  26. width:60px;
  27. border: 2px solid #ffffff;
  28. margin: .35em auto;
  29. }
  30.  
  31. #form-wrapper{
  32. width: 400px;
  33. background: #fff;
  34. padding: .35em .5em;
  35. margin: 1em auto;
  36. border:1px solid #737373f3;
  37. box-shadow: 0px 0px 7px #00000053;
  38. }
  39.  
  40. /* Input Container */
  41. .input-container {
  42. padding: 0.5em 0.35em;
  43. }
  44.  
  45. /* Input Container label */
  46. .input-container label {
  47. display: block;
  48. width: 100%;
  49. font-size: .9rem;
  50. font-weight: 400;
  51. color: #404040;
  52. margin-bottom:.5em;
  53. }
  54.  
  55. /* Input Container label */
  56. .input-container input {
  57. padding: 0.35em 0.5em;
  58. width: 100%;
  59. }

JavaScript

Lastly, here's the JavaScript file script known as script.js. The file contains the codes that make this Weight Converter Application functional.

  1. // Kilogram input selector
  2. const kg = document.getElementById('kg')
  3.  
  4. // Gram input selector
  5. const g = document.getElementById('g')
  6.  
  7. // Pounds input selector
  8. const lbs = document.getElementById('lbs')
  9.  
  10. // Compute Unit convesions
  11. const computeConversion = (fromUnit = '') => {
  12. if(fromUnit != ''){
  13. if(fromUnit == 'kg'){
  14. /**
  15.   * Formula
  16.   * to g = kg / 1000
  17.   * to lbs = kg * 2.2
  18.   */
  19. var grams = kg.value * 1000
  20. var pounds = kg.value * 2.2
  21. g.value = grams
  22. lbs.value = pounds
  23.  
  24. }
  25. if(fromUnit == 'g'){
  26. /**
  27.   * Formula
  28.   * to kg = kg * 1000
  29.   * to lbs = kg * 2.2
  30.   */
  31. var kilograms = g.value / 1000
  32. var pounds = kilograms * 2.2
  33. kg.value = kilograms
  34. lbs.value = pounds
  35. }
  36. if(fromUnit == 'lbs'){
  37. /**
  38.   * Formula
  39.   * to kg = lbs / 2.2
  40.   * to g = kg / 1000
  41.   */
  42. var kilograms = lbs.value / 2.2
  43. var grams = kilograms * 1000
  44.  
  45. g.value = grams
  46. kg.value = kilograms
  47. }
  48. }
  49. }
  50.  
  51. // Trigger Conversions when kilograms input has updated
  52. kg.addEventListener('input', e=>{
  53. e.preventDefault();
  54. computeConversion('kg')
  55. })
  56. // Trigger Conversions when grams input has updated
  57. g.addEventListener('input', e=>{
  58. e.preventDefault();
  59. computeConversion('g')
  60. })
  61. // Trigger Conversions when pounds input has updated
  62. lbs.addEventListener('input', e=>{
  63. e.preventDefault();
  64. computeConversion('lbs')
  65. })

Snapshots

Here are some of the snapshots of the overall result of the web application scripts I have provided above.

Page Interface

Weight Converter Application using Pure JavaScript

Converter Input Panel

Weight Converter Application using Pure JavaScript

There you go! I have also provided the complete source code zip file on this website and it is free to download. The download button is located below this tutorial's content. Feel free to download and enhance the source code the way you desire to enhance your programming capabilities or meet your own requirements.

How to Run the Source Code?

  1. Download the provided source code zip file. (download button is located below)
  2. Extract the downloaded source code zip file.
  3. Locate the index.html file in the extracted source code directory.
  4. Browse the index file with your preferred browser.

DEMO VIDEO

That's it! I hope this Weight Converter Application using Pure JavaScript Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment