Skip to content

Instantly share code, notes, and snippets.

@shaneriley
Created April 23, 2011 01:12
Show Gist options
  • Save shaneriley/938084 to your computer and use it in GitHub Desktop.
Save shaneriley/938084 to your computer and use it in GitHub Desktop.
jQuery Placeholder Attribute Polyfill
if (!("placeholder" in document.createElement("input"))) {
$("input[placeholder]").each(function() {
var $e = $(this),
placeholder = $e.attr("placeholder");
$e.val(placeholder);
$e.bind("focus blur", function(e) {
if (e.type === "focus" && $e.val() === placeholder) { $e.val(""); }
else { if (!$e.val()) { $e.val(placeholder); } }
});
}).closest("form").submit(function() {
var $inputs = $(this).find("input[placeholder]");
$inputs.each(function() {
var $i = $(this);
$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