How to create a word counter in JavaScript

A word counter is a tool that you can use to count the number of words in a piece of text. You can use it to check the length of your document or to keep track of whether you're meeting the word limit.

You can create your own word counter using HTML, CSS, and JavaScript. Open the word counter in your web browser, enter your text in the input field and see how many words you've written.

This project can also be useful in helping you practice and strengthen your JavaScript knowledge.

How to create a user interface for a word counter

To create a user interface for the word counter, add a textarea input to the basic HTML page. This is where you can enter the sentence or paragraph for which you want the word count.

1. Create a new HTML file named "index.html".

2. Inside the file, add the basic structure for the HTML website:

         Word Counter                  

Count Words

3. Inside the container div and below the heading, add a textarea:

4. Below the textarea, add a button:

5. Add a span tag to display the word count when the user clicks on the button above:

     Words: 0 

6. In the same folder as your HTML file, create a new file named "styles.css".

7. Fill in the CSS file to style your website:

body {   margin: 0;   padding: 0;   background-color: #f0fcfc; } * {   font-family: "Arial", sans-serif; } .container {   padding: 100px 25%;   display: flex;   flex-direction: column;   line-height: 2rem;   font-size: 1.2rem;   color: #202C39; } textarea {   padding: 20px;   font-size: 1rem;   margin-bottom: 40px; } button {   padding: 10px;   margin-bottom: 40px; } 

8. Link the CSS file to the HTML file by including the link tag inside the HTML head tag:

9. To test the website's user interface, click on the "index.html" file to open it in a web browser.

How to create a word counter in JavaScript Picture 1

How to count each word in a text area

When a user types a sentence into the text area, the website counts each word as they click the Count Words button. You can add this functionality inside a new JavaScript file.

1. In the same folder as the "index.html" and "styles.css" files , add a new file named "script.js".

2. Link the HTML file to your JavaScript file by adding a script tag at the end of the body tag:

3. Inside script.js, use the getElementById() function to retrieve the textarea, button, and span HTML elements. Store these elements into 3 separate variables:

let input = document.getElementById("input"); let button = document.getElementById("count-button"); let wordCountResult = document.getElementById("word-count-result");

4. Add a click event handler. This function will execute when the user clicks the Count Words button :

button.addEventListener("click", function() { });

5. Inside the click event handler, get the value the user entered in the text area. You can find this value in the input variable, which stores the HTML textarea element.

let str = input.value;

6. Use the split() function to split the string into separate words. This will happen whenever there are spaces in the string. This will store each word as an element of a new array. For example, if the entered sentence is "I love dogs", the resulting array will be ["I", "love", "dogs"].

let wordsList = str.split(" ");

7. Find the number of words using the length of the array:

let count = wordsList.length;

8. To display the results again to the user, change the content of the span HTML element to display the new value:

wordCountResult.innerHTML = count; 

How to use sample word counter

You can check your word counter using a web browser.

1. Open index.html in any web browser.

How to create a word counter in JavaScript Picture 2

2. Enter a sentence or paragraph into the text area:

How to create a word counter in JavaScript Picture 3

3. Click the Count Words button to update the word count. This will display the word count, just like when you check the word count on Google Docs, Microsoft Word, or any other editor with a word count function.

How to create a word counter in JavaScript Picture 4

Now you hopefully have a basic understanding of how to use JavaScript to count words and interact with elements on an HTML page. To improve your programming understanding, keep creating useful small projects using JavaScript.

4 ★ | 2 Vote

May be interested

  • Introduction to 2D Array - 2-dimensional array in JavaScriptIntroduction to 2D Array - 2-dimensional array in JavaScript
    in the following article, we will introduce and show you some basic operations to create and access 2-dimensional arrays - 2d array in javascript. essentially, a 2-dimensional array is a concept of a matrix of matrices - a matrix, used to store information. each 1 element contains 2 separate indexes: row (y) - column and column (x) - column.
  • What is Currying in Javascript? How to use Currying in JavaScriptWhat is Currying in Javascript? How to use Currying in JavaScript
    the currying feature in javascript can help you keep your code tidy and give you a new way of seeing how functions work. currying is ideal when you want to break complex logic into smaller, manageable, and self-contained pieces of code.
  • Create a quick dot (……………) line in Microsoft WordCreate a quick dot (……………) line in Microsoft Word
    tipsmake.com will guide you how to create a 3-dot line in microsoft word 2016, word 2013, word 2010 and older word such as word 2007, 2003 quickly by using keyboard shortcuts.
  • Things to know about 'this' in JavaScriptThings to know about 'this' in JavaScript
    are you having trouble understanding the keyword 'this' in javascript ? then please read what you need to know about 'this' in javascript below.
  • Udemy's top 5 JavaScript coursesUdemy's top 5 JavaScript courses
    a programming language that runs on any computer in the world. a language does not need any special software to run. a language ranked among the top in the world.
  • Image Map in JavaScriptImage Map in JavaScript
    you can use javascript to create image map at client-side. image maps are activated by the usemap attribute for the tag and are defined by special extension tags and.
  • JavaScript code to create Pareto charts & graphsJavaScript code to create Pareto charts & graphs
    the example below illustrates a pareto chart template created with javascript. you will also have the source code to edit in the browser or save to your computer to run locally.
  • Top site with many good JavaScript exercises to practiceTop site with many good JavaScript exercises to practice
    many people, after taking javascript courses and being equipped with some knowledge of the language they pursue, are eager to improve and cultivate these learned skills. so this article will give you a list of the top 3 websites to actually make javascript.
  • ! = and! == What is the difference in JavaScript?! = and! == What is the difference in JavaScript?
    javascript includes operators like in other languages. an operator performs some operations on one or more operands (data values) and produces a result. today's article will help readers learn about 2! = and! == operators in javascript.
  • How to make PIN Pad using HTML CSS & JavaScriptHow to make PIN Pad using HTML CSS & JavaScript
    this article will show you how to implement a pin entry table and check whether it is valid or not with the help of html, css, and javascript.