Saturday, April 1, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home JavaScript

How to Create a Word Counter in JavaScript

learningcode_x1mckf by learningcode_x1mckf
January 13, 2023
in JavaScript
0
How to Create a Word Counter in JavaScript
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


Construct this convenient little instrument for your self and be taught a bit of bit about JavaScript alongside the best way.

Two computer monitors, side-by-side, the one on the left displaying code in an editor


A phrase counter is a instrument that you should utilize to rely the variety of phrases in a bit of textual content. You need to use it to verify the size of a doc or to trace whether or not you’re assembly a phrase rely restrict.


You possibly can create your individual phrase counter utilizing HTML, CSS, and JavaScript. Open your phrase counter in an internet browser, enter your textual content into an enter area, and see what number of phrases you’re utilizing.

This venture can be helpful in serving to you follow and solidify your JavaScript information.


Tips on how to Create the UI for the Phrase Counter

To create the UI for the phrase counter, add a textual content space enter to a fundamental HTML web page. That is the place you’ll be able to enter the sentence or paragraph that you just want to rely the phrases for.

  1. Create a brand new HTML file referred to as “index.html”.
  2. Contained in the file, add the essential construction for an HTML webpage:
     <!doctype html>
    <html lang="en-US">
      <head>
        <title> Phrase Counter </title>
      </head>
      <physique>
        <div class="container">
          <h1> Rely Phrases </h1>
        </div>
      </physique>
    </html>
  3. Contained in the container div and beneath the heading, add a textual content space:
     <textarea id="enter" rows="10"></textarea> 
  4. Beneath the textual content space, add a button:
     <button id="count-button">Rely Phrases</button> 
  5. Add a span tag to show the phrase rely when the person clicks on the button above:
     <div>
        Phrases: <span id="word-count-result">0</span>
    </div>
  6. In the identical folder as your HTML file, create a brand new file referred to as “kinds.css”.
  7. Populate the CSS file with some CSS to fashion your webpage:
     physique 
      margin: 0;
      padding: 0;
      background-color:

    *
      font-family: "Arial", sans-serif;

    .container
      padding: 100px 25%;
      show: flex;
      flex-direction: column;
      line-height: 2rem;
      font-size: 1.2rem;
      shade:

    textarea
      padding: 20px;
      font-size: 1rem;
      margin-bottom: 40px;

    button
      padding: 10px;
      margin-bottom: 40px;

  8. Hyperlink the CSS file to your HTML file by together with a hyperlink tag contained in the HTML head tag:
     <hyperlink rel="stylesheet" href="kinds.css">  
  9. To check the UI of the webpage, click on on the “index.html” file to open it in an internet browser.
    UI for word counter open in the browser

Tips on how to Rely Every Phrase Contained in the Textarea

When a person enters a sentence into the textual content space, the webpage ought to rely every phrase once they click on on the Rely Phrases button.

You possibly can add this performance inside a brand new JavaScript file. If it’s essential, revise different beginner JavaScript project ideas if it’s essential brush up in your JavaScript information.

  1. In the identical folder as your “index.html” and “kinds.css” information, add a brand new file referred to as “script.js”.
  2. Hyperlink your HTML file to your JavaScript file by including a script tag on the backside of the physique tag:
     <physique>
        
        <script src="script.js"></script>
    </physique>
  3. Inside script.js, use the getElementById() operate to retrieve the textarea, button, and span HTML components. Retailer these components into three separate variables:
     let enter = doc.getElementById("enter");
    let button = doc.getElementById("count-button");
    let wordCountResult = doc.getElementById("word-count-result");
  4. Add a click on occasion listener. This operate will execute when the person clicks on the Rely Phrases button:
     button.addEventListener("click on", operate() 

    );

  5. Inside the clicking occasion listener, get the worth that the person entered into the textarea. You’ll find this worth within the enter variable, which shops the textarea HTML aspect.
     let str = enter.worth; 
  6. Use the break up() operate to separate the string into separate phrases. This may happen each time there’s a area within the string. This may retailer every phrase as a component of a brand new array. For instance, if the sentence entered was “I like canine”, the ensuing array could be [“I”, “love”, “dogs”].
     let wordsList = str.break up(" "); 
  7. Discover the phrase rely by utilizing the size of the array:
     let rely = wordsList.size; 
  8. To show the consequence again to the person, change the content material of the span HTML aspect to show the brand new worth:
     wordCountResult.innerHTML = rely;  

Tips on how to Use the Instance Phrase Counter

You possibly can take a look at your phrase counter webpage utilizing an internet browser.

  1. Open index.html in any internet browser.
    UI for JS word counter
  2. Enter a sentence or paragraph into the textual content space:
    JS word counter with sentence in textarea
  3. Click on on the Rely Phrases button to replace the phrase rely. This may show the phrase rely, similar to when you checked the word count on Google Docs, Microsoft Phrase, or some other editor with a phrase rely.
    JS word counter in browser with updated word count

You might also like

4 Packages for Working With Date and Time in JavaScript – MUO – MakeUseOf

Understanding the Power of Proxy in JavaScript – hackernoon.com

JavaScript vs. TypeScript: What's the difference? – TheServerSide.com

Creating Easy Purposes Utilizing JavaScript

Now you hopefully have a fundamental understanding of tips on how to use JavaScript to rely phrases and work together with components on an HTML web page. To boost your programming understanding, proceed creating small helpful tasks in JavaScript.



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

4 Packages for Working With Date and Time in JavaScript – MUO – MakeUseOf

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

4 Packages for Working With Date and Time in JavaScript  MUO - MakeUseOf Source link

Read more

Understanding the Power of Proxy in JavaScript – hackernoon.com

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Understanding the Power of Proxy in JavaScript  hackernoon.com Source link

Read more

JavaScript vs. TypeScript: What's the difference? – TheServerSide.com

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

JavaScript vs. TypeScript: What's the difference?  TheServerSide.com Source link

Read more

JetBrains updates IDEs for Java, JavaScript, Ruby – InfoWorld

by learningcode_x1mckf
March 31, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

JetBrains updates IDEs for Java, JavaScript, Ruby  InfoWorld Source link

Read more

Virtru Announces First Ever FIPS 140-2 Validated JavaScript … – GlobeNewswire

by learningcode_x1mckf
March 30, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Virtru Announces First Ever FIPS 140-2 Validated JavaScript ...  GlobeNewswire Source link

Read more
Next Post
Strait Up: From Java to Borneo, and everything in between🛻

Strait Up: From Java to Borneo, and everything in between🛻

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Scrolly video JavaScript library – FlowingData

Scrolly video JavaScript library – FlowingData

January 19, 2023
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Java Is Now On The Nintendo 64! – Hackaday

February 8, 2023
Azul detects Java vulnerabilities in production apps

Azul detects Java vulnerabilities in production apps

November 6, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • So why did they decide to call it Java? – InfoWorld
  • Senior Java Developer – IT-Online
  • 4 Packages for Working With Date and Time in JavaScript – MUO – MakeUseOf

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?