Created
July 6, 2012 09:39
-
-
Save netsi1964/3059235 to your computer and use it in GitHub Desktop.
HTML5 placeholder polyfill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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