Last active
September 11, 2015 14:01
-
-
Save jonathanpath/f71d64b806cbae63ce61 to your computer and use it in GitHub Desktop.
Placeholder Polyfill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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