hising.net

jQuery Plugin for using default value as label

Wrote a little plugin for using the default value in text elements as label, you know. Look at the right hand side in the newsletter form for an example.

[sourcecode language='javascript'] $.fn.label = function(options){

var default_settings = {
    clearLabels : true
};

var settings = $.extend(default_settings, options);

return this.each(function(){
    var elm = $(this);
    if($(this).val().length > 0) {
        $.data(elm, 'default', $(this).val());
        elm.focus(function(){
            if($(this).val() == $.data(elm, 'default')) {
                $(this).val('');
            }
        }).blur(function(){
            if($(this).val() == '') {
                $(this).val($.data(elm, 'default'));
            }
        });

        if(settings.clearLabels) {
            $(this.form).submit(function(){
                if($(this).val() == $.data(elm, 'default')) {
                    $(this).val('');
                }
            });
        }
    }
});

} [/sourcecode]

Categories

  • JavaScript

Tags

  • code
  • example
  • JavaScript
  • jQuery
  • plugin