Skip to content

Instantly share code, notes, and snippets.

@navitronic
Created February 1, 2010 04:47
Embed
What would you like to do?
// Extend jQuery to add a function for implementing "Nice Inputs"
jQuery.fn.makeNiceInput = function(){
// Setup the selected elements as nice inputs.
jQuery(this).each(function(){
if(jQuery('label[for='+jQuery(this).attr('id')+']').length > 0){
var MNval = jQuery('label[for='+jQuery(this).attr('id')+']').hide().text();
if(jQuery(this).val() == ''){
jQuery(this).val(MNval);
}
$(this).addClass('isMNice');
}
});
jQuery(this).bind('focus', function(){
if(jQuery(this).hasClass('isMNice') && jQuery(this).val() == jQuery('label[for='+jQuery(this).attr('id')+']').text()){
jQuery(this).val('');
}
});
jQuery(this).bind('blur', function(){
if(jQuery(this).hasClass('isMNice') && jQuery(this).val() == ''){
jQuery(this).val(jQuery('label[for='+jQuery(this).attr('id')+']').text())
}
});
jQuery('form').bind('submit', function(){
jQuery(this).find('.isMNice').each(function(){
if(jQuery(this).val() == jQuery('label[for='+jQuery(this).attr('id')+']').text()){
jQuery(this).val('');
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment