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