Word Count

Word count using javascript

HTML

<div id="border">
            <textarea id='text'></textarea>
        </div>
        <div id="result">
            Words: <span id="wordCount">0</span><br/>
        </div>

CSS

#border {
    border: 1px solid red;
    height: 200px;
    width: 100%;
}
textarea {
    height: 100%;
    width: 100%;
    border: 0px;
}

JavaScript

counter = function() {
    var value = $('#text').val();

    if (value.length == 0) {
        $('#wordCount').html(0);
        return;
    }

    var regex = /\s+/gi;
    var wordCount = value.trim().replace(regex, ' ').split(' ').length;

    $('#wordCount').html(wordCount);
};

$(document).ready(function() {
    $('#text').change(counter);
    $('#text').keydown(counter);
    $('#text').keypress(counter);
    $('#text').keyup(counter);
    $('#text').blur(counter);
    $('#text').focus(counter);
});