Skip to content

Instantly share code, notes, and snippets.

@ixzy24
Created June 5, 2012 20:30
Show Gist options
  • Save ixzy24/2877608 to your computer and use it in GitHub Desktop.
Save ixzy24/2877608 to your computer and use it in GitHub Desktop.
html5 placeholder fallback
// jQuery code
var i = document.createElement("input");
// Only bind if placeholder isn't natively supported by the browser
if (!("placeholder" in i)) {
$("input[placeholder]").each(function () {
var self = $(this);
self.val(self.attr("placeholder")).bind({
focus: function () {
if (self.val() === self.attr("placeholder")) {
self.val("");
}
},
blur: function () {
var label = self.attr("placeholder");
if (label && self.val() === "") {
self.val(label);
}
}
});
});
}
<!-- html5 -->
<input type="text" name="search" placeholder="Search" value="">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment