Text counter words and characters

mixanikos365

Text counter words and characters ( paste your text here: )  

You have 0 words and 0 characters


Code:

<textarea cols="105" id="textCount" name="textCount" rows="6"></textarea>
<p>You have <span id="wordCount">0 words</span> and <span id="charCount">0 characters<span></span></span></p><!--more--><p></p><p><span><br /></span></p><p><span><br /></span></p>

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

<script type="text/javascript">
//<![CDATA[


var wordCounter = {
  init: function() {
    this.DOM();
    this.events();
  },
  DOM: function() {
    this.textbox = $("#textCount");
    this.wordCount = $("#wordCount");
    this.charCount = $("#charCount");
  },
  events: function() {
    this.textbox.on("input", this.count.bind(this));
  },
  count: function() {
    var words = this.textbox.val().split(" "),
      chars = this.textbox.val();

    //DELETE EMPTY STRINGS
    for (var i = 0; i < words.length; i++) {
      while (words[i] === "") {
        words.splice(i, 1);
      }
    }
    //COUNT WORDS
    if (words.length === 1) {
      this.wordCount.text(words.length + " word");
    } else {
      this.wordCount.text(words.length + " words");
    }
    //COUNT CHARACTERS
    if (chars.length < 0) {
      words = [];
    } else if (chars.length === 1) {
      this.charCount.text(chars.length + " character");
    } else {
      this.charCount.text(chars.length + " characters");
    }
  }
}

wordCounter.init();


//]]>
</script>
Tags

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !