Skip to content

Instantly share code, notes, and snippets.

@peol
Forked from naterkane/project.js
Created March 29, 2010 08:44
Show Gist options
  • Save peol/347608 to your computer and use it in GitHub Desktop.
Save peol/347608 to your computer and use it in GitHub Desktop.
$(function() {
// Clone HTML5's placeholder attribute functionality
$('input[placeholder]').each(function() {
// Make sure we cache the selector to fasten up the process
var $t = $(this);
// Store the placeholder text and then set the element's value
// TODO: Check to make sure value is empty before setting here
$t.data( 'placeholder', $t.attr('placeholder') );
$t.val( $t.data('placeholder') ).addClass('placeholder');
// Handle the removal/addition of the placeholder text on focus/blur
$t.focus(function() {
if ( $t.val() == $t.data('placeholder') ) {
$t.val('').removeClass('placeholder');
}
}).blur(function() {
if ( ! $t.val().match(/\W/) ) {
$t.val( $t.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