JavaScript - Simple Counting Words

Learn on how to create a Simple Counting Words using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It can renders web pages in an interactive and dynamic fashion. It is widely used in designing a stunning website. So Let's do the coding...

Getting started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Simple Counting Words</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-3"></div>   
  17.                 <div class="col-md-6">
  18.                         <div class="form-group">
  19.                                 <textarea class="form-control" id="words" style="resize:none; height:150px;"></textarea>
  20.                         </div>
  21.                         <center>
  22.                                 <button  class="btn btn-primary" onclick="count()">Count Words</button>    
  23.                                 <button  class="btn btn-success" onclick="reset()">Reset</button>    
  24.                         </center>
  25.                         <br />
  26.                         <div id="result"></div>
  27.                 </div>         
  28.                        
  29.         </div>
  30. <script src="js/script.js"></script>   
  31. </body>
  32. </html>

Creating the Script

This code contains the script of the application. This code will count the words in the sentence when clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. function count() {
  2.         var words = document.getElementById("words").value;
  3.         var count = parseInt(words);
  4.         count_word=words.split(" ");
  5.         document.getElementById("result").innerHTML =
  6.         "<center>The number of words in the sentence has<br /><label class='text-primary' style='font-size:35px;'>" +
  7.         count_word.length + "</label>words</center>";
  8. }
  9. function reset(){
  10.         document.getElementById("result").innerHTML = "";
  11.         document.getElementById("words").value="";
  12. }
There you have it we successfully created a Simple Counting Words using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment