Simple jQuery Word Character Counter For Text Box Word Counter - Download Simple jQuery Word and Character Counter For Text Box - Word Counter

Download Simple jQuery Word and Character Counter For Text Box – Word Counter

Posted on

This time I will share jQuery Plugin and tutorial about Simple jQuery Word and Character Counter For Text Box – Word Counter, hope it will help you in programming stack.

Simple jQuery Word Character Counter For Text Box Word Counter - Download Simple jQuery Word and Character Counter For Text Box - Word Counter
File Size: 2.19 KB
Views Total: 3454
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   



Word Counter is a minimal jQuery script that adds a simple inline word and character counter to your textarea element.

How to use it:

1. Create a word and character counter next to your textarea element.

1 <textarea name="textCount" id="textCount"></textarea>
2 <p>You have <span id="wordCount">0 words</span> and <span id="charCount">0 characters</span></p>

2. Add the necessary jQuery library to your webpage.

1 <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>

3. The core function for the Word and Character Counter.

01 var wordCounter = {
02   init: function() {
03     this.DOM();
04     this.events();
05   },
06   DOM: function() {
07     this.textbox = $("#textCount");
08     this.wordCount = $("#wordCount");
09     this.charCount = $("#charCount");
10   },
11   events: function() {
12     this.textbox.on("input", this.count.bind(this));
13   },
14   count: function() {
15     var words = this.textbox.val().split(" "),
16       chars = this.textbox.val();
17  
18     //DELETE EMPTY STRINGS
19     for (var i = 0; i < words.length; i++) {
20       while (words[i] === "") {
21         words.splice(i, 1);
22       }
23     }
24     //COUNT WORDS
25     if (words.length === 1) {
26       this.wordCount.text(words.length + " word");
27     } else {
28       this.wordCount.text(words.length + " words");
29     }
30     //COUNT CHARACTERS
31     if (chars.length < 0) {
32       words = [];
33     } else if (chars.length === 1) {
34       this.charCount.text(chars.length + " character");
35     } else {
36       this.charCount.text(chars.length + " characters");
37     }
38   }
39 }

4. Initialize the Word and Character Counter.

1 wordCounter.init();

This awesome jQuery plugin is developed by ChynoDeluxe. For more Advanced Usages, please check the demo page or visit the official website.




source : jqueryscript.net