Skip to content

Instantly share code, notes, and snippets.

@alkrauss48
Created June 16, 2014 21:26
Show Gist options
  • Save alkrauss48/d2c2c642e94852cf0f87 to your computer and use it in GitHub Desktop.
Save alkrauss48/d2c2c642e94852cf0f87 to your computer and use it in GitHub Desktop.
Gives your form fields placeholder support for IE8
/* Fake placeholder support in browsers that do not support the HTML input placeholder attribute. Ideally this would be paired with a javascript function that prevents form submit if the a given input's value is equivalent to the placeholder attribute. */
/* Requires both jQuery and Modernizr */
function initPlaceholderSupport() {
if( ! Modernizr.input.placeholder ) {
$('[placeholder]').focus(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur();
}
}
initPlaceholderSupport();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment