Skip to content

Instantly share code, notes, and snippets.

@twaddington
Created March 1, 2011 06:35
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 twaddington/848727 to your computer and use it in GitHub Desktop.
Save twaddington/848727 to your computer and use it in GitHub Desktop.
// Bind input placeholder text
$(document).ready(function() {
// Check for placeholder support
var i = document.createElement('input');
if (('placeholder' in i) == false) {
$('input[placeholder]').each(function() {
// Set initial value and class
if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')) {
$(this).val($(this).attr('placeholder')).addClass('placeholder-text');
}
// Bind handlers
$(this).blur(function() {
if ($(this).val() == '') {
$(this).val($(this).attr('placeholder')).addClass('placeholder-text');
}
}).focus(function() {
if ($(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('placeholder-text');
}
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment