Skip to content

Instantly share code, notes, and snippets.

@ryanahamilton
Created January 12, 2011 22:49
Show Gist options
  • Save ryanahamilton/777062 to your computer and use it in GitHub Desktop.
Save ryanahamilton/777062 to your computer and use it in GitHub Desktop.
cross-browser input placeholder attribute support with jQuery and Modernizr
$(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();
};
})();
<!-- 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