Skip to content

Instantly share code, notes, and snippets.

@umbrae
Created May 24, 2010 18:58
Show Gist options
  • Select an option

  • Save umbrae/412282 to your computer and use it in GitHub Desktop.

Select an option

Save umbrae/412282 to your computer and use it in GitHub Desktop.
/**
* HTML5 Placeholder Deprecation JS.
* Automatically support the placeholder attribute in browsers that don't support it.
*
* To emulate webkit, placeHolderActive class should probably contain something like:
* .placeholderActive { color: darkGray; }
*
* Chris Dary - [email protected]
* Released under the WTFPL License : http://sam.zoy.org/wtfpl/
**/
if (!('placeholder' in document.createElement('input'))) {
var showPlaceholder = function () {
var $this = $(this),
placeholder = $this.attr('placeholder');
if ($this.val() === '' && placeholder) {
$this.val(placeholder).addClass('placeholderActive');
}
};
var hidePlaceholder = function () {
var $this = $(this),
placeholder = $this.attr('placeholder');
if (placeholder && $this.val() === placeholder) {
$this.val('').removeClass('placeholderActive');
}
};
// Look for forms with inputs and/or textareas with a placeholder attribute in them
$('form').submit(function () {
// Clear the placeholder values so they don't get submitted
$('.placeholderActive', this).val('');
});
// Clear placeholder values upon page reload
$(window).unload(function () {
$('.placeholderActive').val('');
});
$(':input').each(showPlaceholder).blur(showPlaceholder).focus(hidePlaceholder);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment