Clear input on focus with jQuery and return on blur

后端 未结 12 2344
耶瑟儿~
耶瑟儿~ 2021-02-09 17:50

This almost works. However, when leaving the field \"defaulttext\" appears rather than the original text value. Not sure how to most efficiently echo the variable inside default

12条回答
  •  一个人的身影
    2021-02-09 18:26

    This is working! I use it myself:

    /* Forminputs löschen und wiederherstellen */
    jQuery(function() {
        var input = jQuery('#userForm input[type=text], #userForm textarea');
    
        input.focus(function() {
    
            jQuery(this).attr('data-default', jQuery(this).val());
            jQuery(this).val('');
    
        }).blur(function() {
            var el = jQuery(this);
    
            if (el.val() == '')
                el.val(el.attr('data-default'));
        });
    });
    

提交回复
热议问题