Last active
October 6, 2015 15:58
-
-
Save dfadler/3018309 to your computer and use it in GitHub Desktop.
Cross Browser Placeholder Fix
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
// 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