Skip to content

Instantly share code, notes, and snippets.

@baghayi-gist
Created November 3, 2012 22:11
Show Gist options
  • Save baghayi-gist/4009068 to your computer and use it in GitHub Desktop.
Save baghayi-gist/4009068 to your computer and use it in GitHub Desktop.
JavaScript: Placeholder html5 shim (jQuery Based)
/* <![CDATA[ */
$(function() {
var input = document.createElement("input");
if(('placeholder' in input)==false) {
$('[placeholder]').focus(function() {
var i = $(this);
if(i.val() == i.attr('placeholder')) {
i.val('').removeClass('placeholder');
if(i.hasClass('password')) {
i.removeClass('password');
this.type='password';
}
}
}).blur(function() {
var i = $(this);
if(i.val() == '' || i.val() == i.attr('placeholder')) {
if(this.type=='password') {
i.addClass('password');
this.type='text';
}
i.addClass('placeholder').val(i.attr('placeholder'));
}
}).blur().parents('form').submit(function() {
$(this).find('[placeholder]').each(function() {
var i = $(this);
if(i.val() == i.attr('placeholder'))
i.val('');
})
});
}
});
/* ]]> */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment