Leandro Vieira Pinho wrote:
Hi,

It´s a simple plugin, that let you clear and reset the value of input
elements.

Example of usage:

$(function() {
 $('input').resetDefaultValue(); // for all input elements
 $('input.className').resetDefaultValue(); // for some elements
 $('#q').resetDefaultValue(); // for a especific element
});

The plugin code:

/**
 * jQuery resetDefaultValue plugin
 * @version 0.9
 * @author Leandro Vieira Pinho <[EMAIL PROTECTED]>
 * How to use
 * $(function() {
 *      $('input').resetDefaultValue(); // for all input elements
 *  $('input.className').resetDefaultValue(); // for some elements
 *      $('#q').resetDefaultValue(); // for a especific element
 * });
 * ChangeLog
 * 0.1 - First relase
 * 0.2 - Filter input elements with type igual text, just it.
 * 0.9 - Rewrite all the plugin code. Thanks to Klaus Hartl [
http://groups.google.com/group/jquery-en/browse_thread/thread/5ca50fd021062331
]
 */
jQuery.fn.resetDefaultValue = function() {
        function _clearDefaultValue() {
                var _$ = $(this);
                if ( _$.val() == this.defaultValue ) { _$.val(''); }
        };
        function _resetDefaultValue() {
                var _$ = $(this);
                if ( _$.val() == '' ) { _$.val(this.defaultValue); }
        };
        return
this.filter('[EMAIL 
PROTECTED]').click(_clearDefaultValue).focus(_clearDefaultValue).blur(_resetDefaultValue);
}

The resetDefaultValue plugin has borned here:
http://groups.google.com/group/jquery-en/browse_thread/thread/5ca50fd021062331


Leandro,

another thought: I think it is reasonable to filter the result set for the type, but you're also returning the filtered set, which kind of breaks chainability and might give unexpected results, as some elements are missing after your plugin's method in the chain.

That said, here's what I propose:

jQuery.fn.resetDefaultValue = function() {
        function _clearDefaultValue() {
                var _$ = $(this);
                if ( _$.val() == this.defaultValue ) { _$.val(''); }
        };
        function _resetDefaultValue() {
                var _$ = $(this);
                if ( _$.val() == '' ) { _$.val(this.defaultValue); }
        };
        
        this
            .filter('[EMAIL PROTECTED]')
            .click(_clearDefaultValue)
            .focus(_clearDefaultValue)
            .blur(_resetDefaultValue);
        return this;
}

Ah, or just add an end() at the end of the chain, that's shorter:

        return this
            .filter('[EMAIL PROTECTED]')
            .click(_clearDefaultValue)
            .focus(_clearDefaultValue)
            .blur(_resetDefaultValue)
            .end();


Cheers, Klaus



Reply via email to