Skip to content

Instantly share code, notes, and snippets.

@maxxcrawford
Created April 14, 2014 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxxcrawford/10684416 to your computer and use it in GitHub Desktop.
Save maxxcrawford/10684416 to your computer and use it in GitHub Desktop.
Placeholder Shim (Modernizr Test)
/* global Modernizr */
(function (app, $, window, document, undefined) {
/* SHIM: <input> placeholder shim
--------------------------------------------------------------------------- */
if(!Modernizr.input.placeholder){
$(document).on('focus', 'input[placeholder], textarea[placeholder]', function() {
var input = $(this);
if (input.val() === input.attr('placeholder')) {
input.val('').removeClass('placeholder');
}
}).on('blur', 'input[placeholder], textarea[placeholder]', function() {
var input = $(this);
if (input.val() === '' || input.val() === input.attr('placeholder')) {
input.addClass('placeholder').val(input.attr('placeholder'));
}
}).trigger('blur');
// KLUDGE: IE8 doesn't seem to trigger 'blur' event on initial load; 10ms trigger delay
setTimeout(function(){ $('input[placeholder], textarea[placeholder]').trigger('blur'); }, 10);
$('[placeholder]').closest('form').submit(function() {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() === input.attr('placeholder')) {
input.val('');
}
});
});
}
})(window.app = window.app || {}, jQuery, window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment