Skip to content

Instantly share code, notes, and snippets.

@roose
Created May 24, 2012 12:30
Show Gist options
  • Select an option

  • Save roose/2781312 to your computer and use it in GitHub Desktop.

Select an option

Save roose/2781312 to your computer and use it in GitHub Desktop.
Cross-Browser Support for HTML5 Placeholder Text in Forms
$.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) $.support.placeholder = true;
if(!$.support.placeholder) {
var active = document.activeElement;
$(':text').focus(function () {
if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(function () {
if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
$(':text').blur();
$(active).focus();
$('form').submit(function () {
$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment