Skip to content

Instantly share code, notes, and snippets.

@danielgolden
Last active December 17, 2015 20:59
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 danielgolden/5671133 to your computer and use it in GitHub Desktop.
Save danielgolden/5671133 to your computer and use it in GitHub Desktop.
A polyfill for the input placeholder attribute.
/**
* placefill.js - A polyfill for the placeholder attribute.
* @author Daniel Golden <danielgolden90@gmail.com>
*/
var $place_holder_inputs = $('input[placeholder]');
var place_holder_text = $('input').attr('placeholder');
// make value attribute of all inputs with placeholder attribute equal to
// value of placeholder attribute
$($place_holder_inputs).each(function () {
$(this).val($(this).attr('placeholder'));
});
// when input with placeholder attribute is focused on, if
// the value is equal to the placeholder attribute
// then make then empty the value of the input
$('input[placeholder]').focus(function () {
if($(this).val() == $(this).attr('placeholder')) {
$(this).val('');
}
});
// when input with placeholder attribute is blured, if the
// input value is blank then make the input value
// equal to the contents of the placeholder attribute
$('input[placeholder]').blur(function () {
if($(this).val() == "") {
$(this).val($(this).attr('placeholder'));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment