Skip to content

Instantly share code, notes, and snippets.

@netsi1964
Created July 6, 2012 09:39
Show Gist options
  • Save netsi1964/3059235 to your computer and use it in GitHub Desktop.
Save netsi1964/3059235 to your computer and use it in GitHub Desktop.
HTML5 placeholder polyfill
// Version 1.0 - 2012/07/06, by Sten Hougaard, http://twitter.com/netsi1964
// Handles browsers which do not support HTML5 placeholder attribute
// Will setup code which emulates the HTML5 placeholder feature
// Require jQuery 1.7.1+
// Defaults to CSS selector which hits any element using "placeholder"
// USE: placeholderPolyfill([selector])
function placeholderPolyfill(sSelector) {
var bSupportsPlaceholder = 'placeholder' in document.createElement('input');
if (!!bSupportsPlaceholder) return;
jQuery((typeof sSelector!='undefined') ? sSelector : '[placeholder]').each(function() {
var $this = jQuery(this);
if (!bSupportsPlaceholder) {
var placeholderValue = $this.attr('placeholder');
$this.val(placeholderValue);
$this.on('focus', function() {
if ($this.val()===placeholderValue) $this.val('');
}).on('blur', function() {
if ($this.val()==='') $this.val(placeholderValue);
});
};
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment