Home > JavaScript, jquery > jQuery: Function to auto-capitalize on ‘keyup’ event

jQuery: Function to auto-capitalize on ‘keyup’ event

November 16, 2010

While putting the finishing touches on a view today, the customer asked if we could force the user’s CAPS LOCK on for a case-specific field. Well, no, not necessarily, but we could caps the characters as they go in.

This should be easy enough, right?  Right!

The function:

jQuery.fn.autoCap = function () {
    $(this).each(function () {
        $(this).bind('keyup', function () {
            $(this).val($(this).val().toUpperCase());
        });
    });
}

Usage:

$("#address-state").autoCap();

It’s easy enough to bind to the event and fire off toUpperCase(). There may be a more optimal way, but this seems to fit the bill and profiled well.

Is there a better way?🙂

Categories: JavaScript, jquery Tags: ,
%d bloggers like this: