Creating a Simple Counter in JavaScript Tutorial

In this tutorial, I will share with you a simple web app written in HTML, CSS, and JavaScript. This tutorial objective is to give new programmers, especially the students a very simple idea of how do simple applications are being made using JavaScript. Here, students can learn about JavaScript's click event listener, page onLoad, and updating textContent. I will be providing a source code of Simple Counter App.

The Simple Counter App allows users to update the displayed count number. The main goal of the application is to have a display content of the current count and buttons for triggering the number count to add or deduct.

Creating the Application Interface

The script below is an HTML code that contains all the elements for the user interface. Open a text-editor such as notepad++, sublime, or vscode. Next, copy the script below and paste it into a new html file. Then, save the file as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Simple Counter App</title>
  8.     <link rel="stylesheet" href="style.css">
  9.     <script src="script.js"></script>
  10. </head>
  11.  
  12.     <main>
  13.         <h1 id="app-title">Simple Counter App in JavaScript</h1>
  14.         <div id="counter-field">
  15.             <button type="button" id="left-button">-</button>
  16.             <div id="counter-container">0</div>
  17.             <button type="button" id="right-button">+</button>
  18.         </div>
  19.     </main>
  20. </body>
  21.  
  22. </html>

Creating the StyleSheet

The script below is a CSS (Cascading Style Sheets) which containes the codes for the design of the elements such as the background colors and font sizes. Add new file in your text editor and paste the code below. Save the file as style.css. This is file is being loaded in the index file as you can see it at the line 9 of index.html file.

  1. html,
  2. body {
  3.     height: 100%;
  4.     width: 100%;
  5.     padding: 0!important;
  6.     margin: unset;
  7. }
  8.  
  9. main {
  10.     margin: unset !important;
  11.     height: 100%;
  12.     width: 100%;
  13.     display: flex;
  14.     flex-direction: column;
  15.     align-items: center;
  16.     justify-content: center;
  17.     background-color: antiquewhite;
  18. }
  19.  
  20. #app-title {
  21.     margin: 5rem 3rem;
  22.     text-align: center;
  23.     font-family: cursive;
  24.     font-size: 5rem;
  25. }
  26.  
  27. #counter-field {
  28.     min-height: 20vh;
  29.     width: 100%;
  30.     display: flex;
  31.     flex-direction: row;
  32.     align-items: center;
  33.     justify-content: center;
  34. }
  35.  
  36. #left-button,
  37. #right-button {
  38.     background-color: aquamarine;
  39.     height: 250px;
  40.     width: 250px;
  41.     border-radius: 20px 20px;
  42.     margin: 10px 0;
  43.     display: flex;
  44.     align-items: center;
  45.     justify-content: center;
  46.     font-weight: bolder;
  47.     font-size: 10rem;
  48.     font-family: cursive;
  49.     text-align: center;
  50.     box-shadow: 1px 6px 21px #783333;
  51.     transition: all ease-in .1s;
  52.     cursor: pointer;
  53. }
  54.  
  55. #counter-container {
  56.     background-color: #efc200;
  57.     border-radius: 20px 20px;
  58.     height: 250px;
  59.     width: 250px;
  60.     display: flex;
  61.     align-items: center;
  62.     justify-content: center;
  63.     margin: 0 50px;
  64.     font-size: 10vw;
  65.     overflow-wrap: anywhere;
  66.     text-align: center;
  67.     box-shadow: 1px 6px 21px #783333;
  68. }
  69.  
  70. #left-button:active,
  71. #right-button:active {
  72.     transform: scale(.9)
  73. }

Creating the JavaScript

The following script is the main script of the application. It contains the scripts that add and deduct button function. Using the click event listener, it allows to trigger the script when the button are being clicked. Save the js file as script.js. This file is being loaded also in the index file at the line 10.

  1. window.counter = function(operator = "plus") {
  2.     var currentCount = document.getElementById('counter-container').textContent
  3.     currentCount = parseInt(currentCount.replace(/,/gi, ""));
  4.     if (operator == "plus") {
  5.         currentCount += 1;
  6.     } else if (operator == 'minus') {
  7.         currentCount -= 1;
  8.     } else {
  9.         alert("undfined opereater.")
  10.         return false;
  11.     }
  12.     document.getElementById('counter-container').textContent = parseInt(currentCount).toLocaleString('en-US')
  13.  
  14. }
  15.  
  16. window.onload = function() {
  17.  
  18.     // Add ("+") button eventListener
  19.     document.getElementById('right-button').addEventListener('click', function() {
  20.         counter();
  21.     })
  22.  
  23.     // Minus ("-") button eventListener
  24.     document.getElementById('left-button').addEventListener('click', function() {
  25.         counter("minus");
  26.     })
  27. }

That's it. You can now test the application on your end. Browse the application into your preferred browser such as Chrome Browser. If you have encountered any error occurs, you can download the working source code I provided in this article. The download button is located below. Then, review your codes and differentiate them from the code I provided. If it still won't work, feel free to leave your queries in the comment section of this article.

DEMO VIDEO

That's the end of this tutorial. I hope this tutorial will help you to understand or give you an idea of how to create an application using HTML, CSS, and JavaScript. Explore more on this website for more Tutorials and Free Source Codes.

Enjoy :)

Add new comment