Creating a Word Counter App in JavaScript Tutorial
In this tutorial, I will show how to create a simple Word Counter App using HTML and JavaScript. The main goal of this tutorial is to give you ideas or techniques how to manipulate html content text and more. This web application idea or techniques can be useful for your future web application projects such as Content Management Systems also known as CMS. The application only uses pure JavaScript codes in acheiving the main features.
The scripts are written in:
- HTML (Hypertext Markup Language)
- CSS (Cascading Style Sheets)
Main Function Scripts:
- JavaScript
UI Framework/Library:
- Bootstrap version 5
About the Word Counter App
The Word Counter App that we will be creating in this tutorial contains a single page that has a text field or text area where the user can input their phrases or content to count. The application will automatically count the number of words, characters, characters without spaces, and paragraphs. The count will automatically initiate or be triggered when the user inputs or enters something on the text field.
Let's do the coding...
Getting Started
In this tutorial script I will be using Bootstrap version 5 a CSS Framework for the user interface design. Kindly download it also in your end.
My source code directory:
- source_code_root_path (./)
- css (./css)
- bootstrap.min.css
- js (.js)
- bootstrap.min.js
- css (./css)
Creating the User Interface
Now, we will be creating our index file which contains the HTML Scripts of the application. This contain the text fields and the count container elements. Open your favorite text editor such as notepad++ or sublime text. Create a new file naming index.html . Copy the script below and paste it ino the index file. Then save the file in your source code root directory.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="./css/bootstrap.min.css">
- <link rel="stylesheet" href="./css/styles.css">
- <style>
- </style>
- </head>
- <body class="bg-dark bg-gradient text-light">
- <main>
- <div class="col-lg-12">
- </div>
- <div class="container w-100">
- <div class="card rounded-0 shadow text-dark">
- <div class="card-body">
- <div class="col-lg-12">
- <div class="row justify-content-center">
- <div class="col-lg-7 col-md-10 col-sm-12 col-sm 12">
- <!-- Count Elements -->
- <div class="row align-items-center">
- <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 px-3">
- </div>
- <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 px-3">
- </div>
- <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 px-3">
- </div>
- <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 px-3">
- </div>
- </div>
- <!-- End of Count Elements -->
- <div class="card rounded-0 shadow">
- <div class="card-body">
- <!-- Text Field Element -->
- <!-- End of Text Field Element -->
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </main>
- </body>
- </html>
Creating the Custom CSS
Next, add a new file naming styles.css. This file contains the script for some additional custom styles for out application interface. In my case, this file is located inside the css directory in my source code folder. This file is included in the index file as you can see it on the line 10 of the index.html file.
- html,
- body {
- min-height: 100%;
- width: 100%;
- }
- main {
- display: block;
- min-height: 100%;
- overflow: auto;
- padding: 1em 0;
- }
- main * {
- font-family: Comic Sans MS;
- }
- #project-title {
- text-shadow: 3px 3px 7px #000;
- padding: 2.5em 1em !important;
- }
Creating the Main Scripts
Lastly, we will creating the JavaScript File that contains the main scripts of the feature of the application. The scripts includes an eventListener scripts and DOM Manipulations. Save this file as script.js. In my case, this file is located inside the js directory of my source code folder. This file is included in the index file as you can see it on the line 12 of the index.html file.
- // Initiate the script below on page window is succesffuly loaded
- window.addEventListener("load", () => {
- (function() {
- // Initiating the count on text field input state
- document.getElementById('text-field').addEventListener("input", function(e) {
- // Text Field Value
- var text = document.getElementById('text-field').value
- // Count Variables
- var total_chars = 0;
- var total_chars_ns = 0;
- var total_words = 0;
- var total_paragraphs = 0;
- // Removing the spaces for counting the characters without the spaces
- var chars = text.replace(/\s/gi, '')
- //Splitting the words using the spaces as the delimiter
- var words = (text.trim()).split(/\s/gi)
- //Splitting the words using the next line as the delimiter
- var paragraphs = (text.trim()).split(/[\n\r]+/gi)
- // removing the empty strings in the word array
- Object.keys(words).map(k => {
- if (words[k].trim() == "") {
- delete words[k];
- }
- })
- words = words.filter((v) => {
- return v.length > 0;
- })
- // Setting the count values
- total_chars = text.length
- total_chars_ns = chars.length
- total_words = words.length
- total_paragraphs = paragraphs.length
- // Updating the count containers text content
- document.getElementById('total-characters').innerText = parseFloat(total_chars).toLocaleString('en-US')
- document.getElementById('total-characters-ns').innerText = parseFloat(total_chars_ns).toLocaleString('en-US')
- document.getElementById('total-words').innerText = parseFloat(total_words).toLocaleString('en-US')
- document.getElementById('total-paragraphs').innerText = parseFloat(total_paragraphs).toLocaleString('en-US')
- })
- })();
- })
That's it. You can now test the application on your end by browsing the index.file in your prefered browser. To know if the application is properly working, check the count containers above the text field. The count containers must update automatically every time you enter or input something on the text field.
DEMO VIDEO
That is the end of this tutorial. If there's an error occurred on your end. You can download the working source code that I created for this tutorial. The download button is located below this article. I hope this will help you with what you are looking for and you'll find something useful for your future web application projects.
Explore more on this website for more Tutorials and Free Source Codes.
Enjoy :)
Add new comment
- 401 views