Created
December 19, 2012 02:15
-
-
Save stephensprinkle-zz/4333818 to your computer and use it in GitHub Desktop.
Cross Browser Default Input Values Script via Jquery
This file contains hidden or 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
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