Live Word Counter using HTML, CSS & JavaScript
Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Live Word Counter which will count the characters and words present in the textbox and it would be live which means it will count each and every letter without any lag. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Live Word Counter Project.
Project Description
Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Live Word Counter Project.
Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Live Word Counter Project.
Step 3
At last we will use JS (JavaScript) which will add a logic to make the Live Word Counter Project responsive from the user end.
I hope you have got an idea about the project.
HTML Code for Live Word Counter
First we’ll start with creating the structure of the Live Word Counter project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.
<html lang="en"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <!-- Google Font --> <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap" rel="stylesheet" /> <!-- Stylesheet --> <link rel="stylesheet" href="style.css" /> </head> <body> <div class="wrapper"> <div class="container"> <textarea id="input-textarea" rows="12" placeholder="Start Typing here..." ></textarea> </div> <div class="count"> <div> <h5 id="word-count">0</h5> <p>Words</p> </div> <div> <h5 id="charac-count">0</h5> <p>Characters</p> </div> </div> </div> <!-- Script --> <script src="script.js"></script> </body> </html>
100+ JavaScript Projects With Source Code ( Beginners to Advanced)
CSS Code for Live Word Counter
Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Live Word Counter project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.
* { padding: 0; margin: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } body { background-color: #8b5ceb; } .wrapper { position: absolute; width: 90vmin; transform: translate(-50%, -50%); top: 50%; left: 50%; } .container { background-color: #ffffff; padding: 50px 30px 80px 30px; border-radius: 8px; box-shadow: 0 30px 50px rgba(30, 21, 49, 0.3); } textarea { width: 100%; border: none; resize: none; outline: none; font-size: 16px; line-height: 28px; color: #0e101a; } .count { background-color: #000000; width: 80%; padding: 20px; position: relative; transform: translate(-50%, -50%); left: 50%; display: flex; justify-content: space-around; text-align: center; border-radius: 5px; box-shadow: 0 20px 40px rgba(30, 21, 49, 0.4); } .count p { color: #a0a0c0; } .count h5 { color: #ffffff; font-size: 32px; }
5+ HTML CSS Projects With Source Code
JavaScript Code for Live Word Counter
Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project Live Word Counter using HTML, CSS & JavaScript (Source Code).
let inputTextArea = document.getElementById("input-textarea"); let characCount = document.getElementById("charac-count"); let wordCount = document.getElementById("word-count"); inputTextArea.addEventListener("input", () => { characCount.textContent = inputTextArea.value.length; let txt = inputTextArea.value.trim(); wordCount.textContent = txt.split(/\s+/).filter((item) => item).length; });
Final Output
We have Successfully created our Live Word Counter using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.
If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.
Written By – Harsh Sawant
Code By – @harshh9