Created
August 3, 2010 14:43
-
-
Save patrys/506492 to your computer and use it in GitHub Desktop.
Fallback code for the HTML5 "placeholder" attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Neat, but could use a bit of work, DRY-wise.