Skip to content

Instantly share code, notes, and snippets.

@spsaucier
Forked from nhunzaker/inline-labels.js
Created September 26, 2013 06:09
Show Gist options
  • Save spsaucier/6710425 to your computer and use it in GitHub Desktop.
Save spsaucier/6710425 to your computer and use it in GitHub Desktop.
// Inline Labels
// -------------------------------------------------- //
$('input, textarea').each(function() {
var self = $(this),
label = $("label[for='" + self.attr("id") + "']").hide().text();
// Replace initial values
if (!self.val().replace(/^\s+/g, "").length) {
self.val(label).addClass("placeholder");
}
self.on("focus", function() {
if (self.val() === label) {
self.val('').removeClass("placeholder");
}
});
self.on("blur", function() {
if (!self.val().replace(/^\s+/g, "")) {
self.val(label).addClass("placeholder");
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment