Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.