Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @samgro samgro revised this gist Feb 14, 2011. 1 changed file with 19 additions and 4 deletions.
    23 changes: 19 additions & 4 deletions jQuery HTML5 placeholder fix.js
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,37 @@
    jQuery.placeholder = function() {
    $('[placeholder]').focus(function() {
    var input = $(this);
    if (input.val() === input.attr('placeholder')) {
    if (input.hasClass('placeholder')) {
    input.val('');
    input.removeClass('placeholder');
    }
    }).blur(function() {
    var input = $(this);
    if (input.val() === '' || input.val() === input.attr('placeholder')) {
    if (input.val() === '') {
    input.addClass('placeholder');
    input.val(input.attr('placeholder'));
    }
    }).blur().parents('form').submit(function() {
    $(this).find('[placeholder]').each(function() {
    var input = $(this);
    if (input.val() === input.attr('placeholder')) {
    if (input.hasClass('placeholder')) {
    input.val('');
    }
    });
    });
    };

    // Clear input on refresh so that the placeholder class gets added back
    $(window).unload(function() {
    $('[placeholder]').val('');
    });
    };

    // If using AJAX, call this on all placeholders after submitting to
    // return placeholder
    jQuery.fn.addPlaceholder = function() {
    return this.each(function() {
    var input = $(this);
    input.addClass('placeholder');
    input.val(input.attr('placeholder'));
    });
    };
  2. @samgro samgro revised this gist Feb 12, 2011. 1 changed file with 18 additions and 16 deletions.
    34 changes: 18 additions & 16 deletions jQuery HTML5 placeholder fix.js
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,22 @@
    $('[placeholder]').focus(function() {
    var input = $(this);
    if (input.val() == input.attr('placeholder')) {
    input.val('');
    input.removeClass('placeholder');
    }
    }).blur(function() {
    var input = $(this);
    if (input.val() == '' || input.val() == input.attr('placeholder')) {
    input.addClass('placeholder');
    input.val(input.attr('placeholder'));
    }
    }).blur().parents('form').submit(function() {
    $(this).find('[placeholder]').each(function() {
    jQuery.placeholder = function() {
    $('[placeholder]').focus(function() {
    var input = $(this);
    if (input.val() == input.attr('placeholder')) {
    if (input.val() === input.attr('placeholder')) {
    input.val('');
    input.removeClass('placeholder');
    }
    }).blur(function() {
    var input = $(this);
    if (input.val() === '' || input.val() === input.attr('placeholder')) {
    input.addClass('placeholder');
    input.val(input.attr('placeholder'));
    }
    }).blur().parents('form').submit(function() {
    $(this).find('[placeholder]').each(function() {
    var input = $(this);
    if (input.val() === input.attr('placeholder')) {
    input.val('');
    }
    });
    });
    });
    };
  3. @samgro samgro revised this gist Feb 11, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jQuery HTML5 placeholder fix.js
    Original file line number Diff line number Diff line change
    @@ -16,5 +16,5 @@ $('[placeholder]').focus(function() {
    if (input.val() == input.attr('placeholder')) {
    input.val('');
    }
    })
    });
    });
  4. @hagenburger hagenburger revised this gist Sep 29, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jQuery HTML5 placeholder fix.js
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ $('[placeholder]').focus(function() {
    }
    }).blur(function() {
    var input = $(this);
    if (input.val() == '') {
    if (input.val() == '' || input.val() == input.attr('placeholder')) {
    input.addClass('placeholder');
    input.val(input.attr('placeholder'));
    }
  5. @hagenburger hagenburger renamed this gist Apr 26, 2010. 1 changed file with 0 additions and 0 deletions.
  6. @hagenburger hagenburger created this gist Apr 26, 2010.
    20 changes: 20 additions & 0 deletions jQuery HTML5 placeholder fix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    $('[placeholder]').focus(function() {
    var input = $(this);
    if (input.val() == input.attr('placeholder')) {
    input.val('');
    input.removeClass('placeholder');
    }
    }).blur(function() {
    var input = $(this);
    if (input.val() == '') {
    input.addClass('placeholder');
    input.val(input.attr('placeholder'));
    }
    }).blur().parents('form').submit(function() {
    $(this).find('[placeholder]').each(function() {
    var input = $(this);
    if (input.val() == input.attr('placeholder')) {
    input.val('');
    }
    })
    });