Skip to content

Instantly share code, notes, and snippets.

@adrianengine
Created January 4, 2013 15:48
Show Gist options
  • Save adrianengine/4453598 to your computer and use it in GitHub Desktop.
Save adrianengine/4453598 to your computer and use it in GitHub Desktop.
A simple snippet to detect placeholder support with jQuery on form inputs and fallback accordingly.
//Find if user browser supports placeholder
jQuery(function() {
jQuery.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) jQuery.support.placeholder = true;
});
if(!$.support.placeholder) {
// If placeholder is not supported
var inputPlaceholder= function () {
var input = $('form').find('input[placeholder]');
input.each(function (index) {
$(this).val($(this).attr('placeholder'));
});
input.on('focus', function () {
if ($(this).val() == $(this).attr('placeholder')) {
$(this).val('');
};
});
input.on('blur', function () {
if ($(this).val() == '') {
$(this).val($(this).attr('placeholder'));
};
});
}
inputPlaceholder();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment