Skip to content

Instantly share code, notes, and snippets.

@bradly
Forked from kevinthompson/clear_input.js
Created August 9, 2011 21:17
Show Gist options
  • Save bradly/1135228 to your computer and use it in GitHub Desktop.
Save bradly/1135228 to your computer and use it in GitHub Desktop.
Clear Default Text Input Value on Focus
// Clear Default Text Input Value on Focus
function hasPlaceholderSupport() {
var input = document.createElement('input');
return ('placeholder' in input);
}
if ( hasPlaceholderSupport() == false ) {
$('input[type="text"]').each(function(){
var def = $(this).attr('placeholder') != '' && $(this).attr('placeholder') != undefined ? $(this).attr('placeholder') : $(this).attr('value');
$(this).val(def).attr('placeholder','');
$(this).focus(function(){
if($(this).val() == def){
$(this).removeClass('placeholder').val('');
}
}).blur(function(){
if($(this).val() == ''){
$(this).addClass('placeholder').val(def);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment