Skip to content

Instantly share code, notes, and snippets.

@kalley
Created January 3, 2013 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalley/4446483 to your computer and use it in GitHub Desktop.
Save kalley/4446483 to your computer and use it in GitHub Desktop.
Add placeholders when necessary and make them act like placeholders. Use CSS to style .placeholder
if ( ! Modernizr.placeholder ) {
(function($) {
$(document).find('input[placeholder]').each(function() {
var el = $(this).on('keyup blur change', function() {
placeholder[this.value.length > 0 ? 'hide' : 'show']();
});
var css = {};
var offset = el.position();
css.left = offset.left + parseFloat(el.css('padding-left')) + parseFloat(el.css('margin-left'));
css.top = offset.top + parseFloat(el.css('padding-top')) + parseFloat(el.css('margin-top'));
var placeholder = $('<span class=placeholder>' + el.attr('placeholder') + '</span>').insertAfter(el).css(css).on('click', function() {
el.trigger('focus');
});
setTimeout(function() {
el.trigger('blur');
}, 13);
});
})(jQuery);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment