Created
January 3, 2013 19:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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