Skip to content

Instantly share code, notes, and snippets.

@sgraber
Created February 7, 2010 02:15
Show Gist options
  • Save sgraber/297134 to your computer and use it in GitHub Desktop.
Save sgraber/297134 to your computer and use it in GitHub Desktop.
// Value swap (usually for search fields)
// whenever you have a search field and you want it to defaultly display a value “search…”
// and have that value empty out on focus, well this is how to do this with jQuery 1.4.
swap_val = [];
$(".swap").each(function(i){
swap_val[i] = $(this).val();
$(this).focusin(function(){
if ($(this).val() == swap_val[i]) {
$(this).val("");
}
}).focusout(function(){
if ($.trim($(this).val()) == "") {
$(this).val(swap_val[i]);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment