Hello friends, Today in this blog I will show you how to create word and character counter using JavaScript. I have created blog on other cool stuffs of web designing & development. Earlier, you have learned to Create a Responsive Landing Page.
Word & Character counter is a tool that will show you the exact number of words & character respectively of certain input field. This will help you a lot while writing essay and articles. Today I’m going to create word and character counter using only JavaScript.
Before writing Javascript code let’s start with designing part. At first create one ‘textarea’ where you can type anything you want and two different divs to show the output number of words and character. After that you will observe the following output:-
As you just seen in the image, the UI is not good looking and user friendly. So, for making its UI eye catching we have to add some CSS. I will provide CSS code at the last part of this blog. After applying CSS, our UI look like this:-
That’s all we have to do in designing part. Now let’s move towards the main and important part where we add functionality for counting words and character using Javascript programming language. At first we will select one input field and two output fields using ‘ID’ and store it in three different variables. Then we will create a ‘Input’ function which will observe if the user type anything inside the textarea. If it found some texts then it will count the words and letters ignoring white spaces. While counting the letters we have to make sure that white spaces are not counting. For that we will replace all the whitespaces by using below code.
char.replace(/\s/g, '');
If you’re feeling difficult to understand what I am saying. You can watch a video preview on this program (Word and Character Counter using JavaScript).
You Might Also Like
Video Preview of Word & Character Counter using Javascript
All this design is based on CSS only and counting functionality is based on JavaScript. If you’re a beginner you can also create this type of counter. If you like this program (Word and Character Counter using JavaScript) and want to get source code. You can easily get the source code of this program. To get the source codes you just need to scroll down.
Word and Character Counter using JavaScript [Source Codes]
To create this program (Word and Character Counter using JavaScript). First, you need to create three Files one HTML File, one CSS File and one JavaScript file. After creating these files just paste the following code in your file. First, create an HTML file with the name of index.html and paste the given codes in your HTML file. Remember, you’ve to create a file with .html extension.
HTML
Second, create a CSS file with the name of style.css and paste the given code in your CSS file. Remember, you’ve to create a file with .css extension.
CSS
At last, create a JavaScript file with the name of counter.js and paste the given code in your JS file. Remember, you’ve to create a file with .js extension.
JavaScript
That’s all, now you’ve successfully created a word and character counter using JavaScript. If your code does not work or you’ve faced any errors/problems then please comment down your queries. If you feel this article had helped you, please share it with your friends. Subscribe to our Website by pressing bell icon at the right bottom. We will send you notification regarding our new posts and other tips and tricks. You can unsubscribe it anytime you like. If you have any suggestions for me please mention it in the comment below.


Post a Comment