How to show/hide input value on focus?

后端 未结 7 1998
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 12:44

I see this all over the web, but was wondering if anyone has the JavaScript code for the EASIEST way to show input value on blur, but hide in on focus.

7条回答
  •  青春惊慌失措
    2021-02-06 12:58

    I prefer jQuery way:

    $(function(){
        /* Hide form input values on focus*/ 
        $('input:text').each(function(){
            var txtval = $(this).val();
            $(this).focus(function(){
                if($(this).val() == txtval){
                    $(this).val('')
                }
            });
            $(this).blur(function(){
                if($(this).val() == ""){
                    $(this).val(txtval);
                }
            });
        });
    });
    

    It is modified Hide Form Input Values On Focus With jQuery by Zack Perdue.

提交回复
热议问题