Login Latest Snippets Home My Account
Snippee! A personal code snippet manager... Store, share, organize! ...

Default values in text fields

Published: 2011-08-11 18:57:50 - In: jQuery

This snippet will enable you how to keep a default value in a text input field for when a user hasn't entered in a value to replace it

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]);
        }
    });
});