Skip to content

Instantly share code, notes, and snippets.

@namklabs
Created March 13, 2012 22:09
Show Gist options
  • Save namklabs/2032094 to your computer and use it in GitHub Desktop.
Save namklabs/2032094 to your computer and use it in GitHub Desktop.
Input element placeholder text with older browser support
;
// attribute test by Jeremy Keith, http://www.abookapart.com/products/html5-for-web-designers
function elementSupportsAttribute( element, attribute ){
var test = document.createElement(element);
if (attribute in test){
return true;
} else {
return false;
}
}
// placeholder dimming for input fields
if( !elementSupportsAttribute('input','placeholder') ){
// add title attributes to inputs - this is for browsers that don't support placeholders
$("input#search").attr("title","Search your website...").attr("value", $("input#search").attr("title") );
$("input#name").attr("title","Enter your name here").attr("value", $("input#name").attr("title") );
// replacement functionality for browsers that don't support placeholder, + dimming
$("input, textarea").not("[type=button],[type=submit]").focus(function(){
$(this).css({"color":"#222222"});
if( $(this).val() == $(this).attr("title") ){
$(this).val("");
}
}).blur(function(){
if( $(this).val() == $(this).attr("title") || $(this).val() == "" ){
$(this).val( $(this).attr("title") );
$(this).css({"color":"#cccccc"});
}
});
} else {
// add placeholder to inputs
$("input#search").attr("placeholder","Search your website...");
$("input#name").attr("placeholder","Enter your name here");
// dim text on text inputs that have placeholder attribute
$("input, textarea").not("[type=button],[type=submit]").focus(function(){
$(this).css({"color":"#222222"});
}).blur(function(){
if( $(this).val() == $(this).attr("placeholder") || $(this).val() == "" ){
$(this).css({"color":"#cccccc"});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment