Created
January 12, 2011 22:49
-
-
Save ryanahamilton/777062 to your computer and use it in GitHub Desktop.
cross-browser input placeholder attribute support with jQuery and Modernizr
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
$(function(){ | |
if (!Modernizr.input.placeholder){ | |
$('#search input[type="search"]') | |
.clearValue(); | |
} | |
}); | |
$.fn.clearValue = function() { | |
var element = this; | |
var defaultStr = $(this).attr('placeholder'); | |
$(this).val(defaultStr); | |
return this.focus(function() { | |
if( this.value == defaultStr ) { | |
this.value = ""; | |
} | |
}).blur(function() { | |
if( !this.value.length ) { | |
this.value = defaultStr; | |
} | |
}).siblings(":submit").click(function() { | |
$(this).siblings(element.selector).each(function() { | |
if( this.value == defaultStr ) { | |
this.value = ""; | |
} | |
}); | |
return true; | |
}).end(); | |
}; | |
})(); |
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
<!-- Include jQuery, Modernizr, and application.js scripts --> | |
<input type="search" name="search" id="search" placeholder="Search" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment