jQuery word counter

by lshettyl

HTML

<textarea id='text'></textarea>

<div id="result">Total words: <span id="wordCount">0</span></div>

CSS

textarea {
    display: block;
    height: 200px;
    width: 100%;
    border: 0px;
    outline: 2px solid green;
}

JavaScript

var countWords = function() {

    var value = $.trim($('#text').val());
    var wordCount = value.length > 0 && (wordCount = value.split(/\s+/g).length) || 0;
    $('#wordCount').html(wordCount);
};

$(function() {
    $('#text').on("change keyup input paste", countWords);
});