Skip to content

Instantly share code, notes, and snippets.

@murtaugh
Created September 20, 2013 15:21
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 murtaugh/6639161 to your computer and use it in GitHub Desktop.
Save murtaugh/6639161 to your computer and use it in GitHub Desktop.
In my experience, `placeholder` polyfills try way too hard, and the results are mixed. Here's mine.
if (!Modernizr.input.placeholder) {
$('*[placeholder]').each(function() {
placeholder = $(this).attr('placeholder');
$(this).attr('value', placeholder).addClass('fakePlaceholder');
});
$('form').on('focus', '.fakePlaceholder', function() {
value = $(this).val();
placeholder = $(this).attr('placeholder');
if (value == placeholder) {
$(this).attr('value', '');
};
});
$('form').on('blur', '.fakePlaceholder', function() {
value = $(this).val();
placeholder = $(this).attr('placeholder');
if (value == '') {
$(this).attr('value', placeholder);
};
});
$('body').on('submit', 'form', function() {
$(this).find('.fakePlaceholder').each(function() {
value = $(this).val();
placeholder = $(this).attr('placeholder');
if (value == placeholder) {
$(this).attr('value', '');
};
});
return true;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment