Skip to content

Instantly share code, notes, and snippets.

@kaelig
Created July 11, 2010 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kaelig/471843 to your computer and use it in GitHub Desktop.
Save kaelig/471843 to your computer and use it in GitHub Desktop.
Applies placeholder attribute behavior in web browsers that don't support it
input {
color: #444;
background: white;
}
input.placeholder {
color: #aaa;
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" charset="utf-8"></script>
<script type="text/javascript">
$(document).ready(function() {
// Applies placeholder attribute behavior in web browsers that don't support it
if (!('placeholder' in document.createElement('input'))) {
$('input[placeholder]').each(function() {
$(this).data('originalText', $(this).val()).data('placeholder', $(this).attr("placeholder"));
if (!$(this).data('originalText').length)
$(this).val($(this).data("placeholder")).addClass('placeholder');
$(this)
.bind("focus", function () {
if ($(this).val() === $(this).data('placeholder'))
$(this).val("").removeClass('placeholder');
})
.bind("blur", function () {
if (!$(this).val().length)
$(this).val($(this).data('placeholder')).addClass('placeholder');
})
.parents("form").bind("submit", function () {
// Empties the placeholder text at form submit if it hasn't changed
if ($(this).val() === $(this).data('placeholder'))
$(this).val("").removeClass('placeholder');
});
});
// Clear at window reload to avoid it stored in autocomplete
$(window).bind("unload", function () {
$('input[placeholder]').each(function(index) {
if ($(this).val() === $(this).data('placeholder'))
$(this).val("");
});
});
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment