spsaucier
9/26/2013 - 6:09 AM

inline-labels.js

// 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");
        }
    });

});