Skip to content

Instantly share code, notes, and snippets.

@dfadler
Last active October 6, 2015 15:58
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 dfadler/3018309 to your computer and use it in GitHub Desktop.
Save dfadler/3018309 to your computer and use it in GitHub Desktop.
Cross Browser Placeholder Fix
// Stores a reference to all input with a placeholder attribute
var $inputs = $('input[placeholder]');
if (!Modernizr.input.placeholder) {
$inputs.each(function(i, el) {
var $el = $(el),
placeholder = $el.attr('placeholder');
// Sets the input value to be the same as the placeholder
$el.attr('value', placeholder);
$el.attr('placeholder', '');
$el.on('blur focus', function(e) {
// Clears the input value if the value is equal to the placeholder text
if (e.type === 'focus') {
if ($el.attr('value') === placeholder) {
$el.attr('value', '');
}
// Replaces the value if nothing was input
} else {
if ($el.attr('value') === '') {
$el.attr('value', placeholder);
}
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment