Skip to content

Instantly share code, notes, and snippets.

@stephensprinkle-zz
Created December 19, 2012 02:15
Show Gist options
  • Save stephensprinkle-zz/4333818 to your computer and use it in GitHub Desktop.
Save stephensprinkle-zz/4333818 to your computer and use it in GitHub Desktop.
Cross Browser Default Input Values Script via Jquery
Note: for each input you have to set both a `placeholder` and a `value`.
Example:
<input name="home_phone"
type="text"
value="Phone Number"
placeholder="Phone Number" />
Function:
function defaultInputValues(){
//Handle Cross Browser Default Input Values
var placeholderSupported = !!( 'placeholder' in document.createElement('input') );
if (placeholderSupported === true){
$('input:not([type="submit"])').val('');
} else{
$("input")
.focus(function() {
if (this.value === this.defaultValue) {
this.value = '';
}
})
.blur(function() {
if (this.value === '') {
this.value = this.defaultValue;
}
});
};
$("select").bind("focus", function () {
$(this).children('option[value="defaultValue"]').remove();
$(this).removeClass('default-value');
});
$("select").addClass('default-value');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment