Skip to content

Instantly share code, notes, and snippets.

@Quinten
Created July 2, 2013 08:01
Show Gist options
  • Save Quinten/5907511 to your computer and use it in GitHub Desktop.
Save Quinten/5907511 to your computer and use it in GitHub Desktop.
clear text/password field on focus Every element that needs to clear on focus should have the class clearfocus
jQuery(document).ready(function () {
var $ = jQuery;
$('.clearfocus').each(function( index ) {
if ($(this).attr('type') == 'password') {
var fakeInput = $('<input type="text" />');
fakeInput.val($(this).val()).addClass('input-text').addClass('passclearfocus');
$(this).after(fakeInput);
$(this).hide();
}
});
$('.clearfocus').bind('focus blur', function (e) {
if (e.type == 'blur' && this.value == ''){
this.value = this.defaultValue;
if ($(this).attr('type') == 'password') {
$(this).hide();
$(this).next().show();
}
}
else if (e.type == 'focus' && this.value == this.defaultValue){
this.value = '';
}
});
$('.passclearfocus').bind('focus', function (e) {
$(this).hide();
$(this).prev().show().focus();
});
$('.clearfocus').blur();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment