Sometimes, when reimplementing, say, an Access database as a Web-based application, you may get a request such as "My Access app used to be able to auto-capitalise words as I type them!"

Here's how to implement such functionality in Javascript:

(function()

{

    /* Handlers for input.ucwords which auto-capitalise 

     * the field contents.

     *

     * Handles any textinput on screen with class 'ucwords'

     */

    function fn(e, t)

    {

        var el = $(e.target);

        if( !el.is("input.ucwords") ) return;


        // Small timeout so this is multi-use between "blur" and "keyup"

        if( !t ) return setTimeout(function(){fn(e, 1)}, 500);


        var words = el.val().split(/ +/);

        for( var x=0; x<words.length; x++ )

            words[x] = ucword(words[x]);

        el.val(words.join(" "));

    }


    function ucword(word)

    {

        return word.substr(0,1).toUpperCase() + word.substr(1).toLowerCase();

    }


    $(document).focusout(fn);

})()

Enjoy.


...Click for More
Access Conversion
Article
Auto Capitalise words
IT
javascript
Programming
UCWords