Skip to content

Instantly share code, notes, and snippets.

@AlexCuse
Forked from aaronmccall/placeholder.js
Created July 10, 2012 17:37
Show Gist options
  • Save AlexCuse/3084976 to your computer and use it in GitHub Desktop.
Save AlexCuse/3084976 to your computer and use it in GitHub Desktop.
shim for browsers that don't support the html5 placeholder attribute
(function () {
$.support.placeholder = false;
var test = document.createElement('input');
if ('placeholder' in test) {
$.support.placeholder = true;
return function () { }
} else {
return function () {
$(function () {
var active = document.activeElement;
$('form').delegate(':text', 'focus', function () {
var _placeholder = $(this).attr('placeholder'),
_val = $(this).val();
if (_placeholder !== undefined && _val == _placeholder) {
$(this).val('').removeClass('hasPlaceholder');
}
}).delegate(':text', 'blur', function () {
var _placeholder = $(this).attr('placeholder'),
_val = $(this).val();
if (_placeholder !== undefined && (_val == '' || _val == _placeholder)) {
$(this).val(_placeholder).addClass('hasPlaceholder');
}
}).submit(function () {
$(this).find('.hasPlaceholder').each(function () { $(this).val(''); });
});
$(':text').blur();
$(active).focus();
});
}
}
})()();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment