Skip to content

Instantly share code, notes, and snippets.

@patrys
Created August 3, 2010 14:43
Show Gist options
  • Select an option

  • Save patrys/506492 to your computer and use it in GitHub Desktop.

Select an option

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.value == text || fld.value == '') {
fld.addClass('jqPlaceholder');
fld.val(text);
}
}
function removePlaceholder() {
if (fld.value == text || fld.value == '') {
fld.val('');
fld.removeClass('jqPlaceholder');
}
}
setPlaceholder();
$(this).focus(removePlacholder);
$(this).blur(setPlaceholder);
$(this).parents("form").submit(removePlacholder);
});
}
});
@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