Skip to content

Instantly share code, notes, and snippets.

@jonathanpath
Last active September 11, 2015 14:01
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 jonathanpath/f71d64b806cbae63ce61 to your computer and use it in GitHub Desktop.
Save jonathanpath/f71d64b806cbae63ce61 to your computer and use it in GitHub Desktop.
Placeholder Polyfill
/**
* Placeholders for old browsers
*/
function placeholderOldBrowsers() {
if (!Modernizr.input.autofocus) {
$('input').each(function(){
if ($(this).attr('autofocus'))
$(this).focus();
});
}
if (!Modernizr.input.placeholder) {
$('input').each(function(){
if ($(this).attr('placeholder')) {
var placeholder_text = $(this).attr('placeholder');
$(this).attr('value', placeholder_text).addClass('nc');
}
});
// for all fields with uncleared initial value, on focus
$('.nc').focus(function() {
// if it is uncleared
if ($(this).hasClass('nc')) {
// remeber the initial value
var $textbox = $(this).val();
// and remove it
$(this).removeClass('nc').val('');
}
}).focusout(function() { // on focus out
// if there is no new value posted by user
if ($(this).val() == '')
// mark as uncleared and show initial value
$(this).addClass('nc').val($textbox);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment