Skip to content

Instantly share code, notes, and snippets.

@lunr
Created March 10, 2011 20:56
Show Gist options
  • Select an option

  • Save lunr/864912 to your computer and use it in GitHub Desktop.

Select an option

Save lunr/864912 to your computer and use it in GitHub Desktop.
jquery-place-holder-support.js
// Extend jQuery.support to test for placeholder support
$.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) $.support.placeholder = true;
// If the placeholder attribute is not supported
// ref: http://www.cssnewbie.com/cross-browser-support-for-html5-placeholder-text-in-forms/
if(!$.support.placeholder) {
// save the active element for future reference
var active = document.activeElement;
// when an input gets focus, remove any placeholder text
$('input[placeholder],textarea[placeholder]').focus(function () {
if ($(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
// when an input loses focus, add placeholder text if it's blank
}).blur(function () {
if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
// remove focus from all inputs, to trigger the placeholder actions
$('input[placeholder],textarea[placeholder]').blur();
// restore focus to the previously active element
$(active).focus();
// when the form is submitted, don't submit placeholder values
$('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