Skip to content

Instantly share code, notes, and snippets.

@walesmd
Created March 26, 2010 19:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save walesmd/345299 to your computer and use it in GitHub Desktop.
Save walesmd/345299 to your computer and use it in GitHub Desktop.
$(function() {
// Clone HTML5's placeholder attribute functionality
$('input[placeholder]').each(function() {
// Store the placeholder text and then set the element's value
// TODO: Check to make sure value is empty before setting here
$(this).data('placeholder', $(this).attr('placeholder'));
$(this).val($(this).data('placeholder')).addClass('placeholder');
// Handle the removal/addition of the placeholder text on focus/blur
$(this).focus(function() {
if ($(this).val() == $(this).data('placeholder')) {
$(this).val('').removeClass('placeholder');
}
}).blur(function() {
if (!$(this).val().match(/\w/)) {
$(this).val($(this).data('placeholder')).addClass('placeholder');
}
});
});
});
input.placeholder {
color:#AAA;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment