Skip to content

Instantly share code, notes, and snippets.

@patrys
Created August 3, 2010 14:43
Show Gist options
  • Save patrys/506492 to your computer and use it in GitHub Desktop.
Save patrys/506492 to your computer and use it in GitHub Desktop.
Fallback code for the HTML5 "placeholder" attribute
$(function() {
if (!('placeholder' in document.createElement('input'))) {
$('input[placeholder], textarea[placeholder]').each(function() {
var text = this.getAttribute('placeholder');
var fld = $(this);
function setPlaceholder() {
if (fld.val() == text || fld.val() == '') {
fld.addClass('jqPlaceholder');
fld.val(text);
}
}
function removePlaceholder() {
if (fld.val() == text || fld.val() == '') {
fld.val('');
fld.removeClass('jqPlaceholder');
}
}
setPlaceholder();
fld.focus(removePlaceholder);
fld.blur(setPlaceholder);
fld.parents("form").submit(removePlaceholder);
});
}
});
@riddle
Copy link

riddle commented Aug 3, 2010

Neat, but could use a bit of work, DRY-wise.

@patrys
Copy link
Author

patrys commented Aug 3, 2010

There you go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment