[jQuery] Follow me to best pratice in create plugin to jQuery
Hi Guys,
I know that already exist a plugin to manage a defaultValue of input
elements. But I create another.
I´m new with jQuery, principally in create plugins. So, I would like
someone look the plugin code and tell me if it´s in a best practice.
Thanks.
The plugin code:
/**
* jQuery defaultValue plugin
* @version 0.1
* @author Leandro Vieira Pinho <leandro.w3invent@gmail.com>
* How to use
* $(function() {
* $('input').defaultValue(); // for all input elements
* $('textarea').defaultValue(); // for all textarea elements
* $('#q').defaultValue(); // for a especific object
* });
*/
jQuery.fn.defaultValue = function() {
this.each( function() {
$(this).click(function() {
clearDefaultValue(this);
});
$(this).focus(function() {
clearDefaultValue(this);
});
$(this).blur(function() {
backDefaultValue(this);
});
function clearDefaultValue(obj) {
if ( $(obj).val() == $(obj)[0].defaultValue ) { $(obj).val(''); }
};
function backDefaultValue(obj) {
if ( $(obj).val() == '' ) { $(obj).val($(obj)[0].defaultValue); }
};
});
};
$(function() {
$('input#leo').defaultValue();
$('textarea').defaultValue();
});