-
-
Save thedaviddias/4195098 to your computer and use it in GitHub Desktop.
Applies placeholder attribute behavior in web browsers that don't support it
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
input { | |
color: #444; | |
background: white; | |
} | |
input.placeholder { | |
color: #aaa; | |
} |
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
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> | |
<script> | |
$(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