Javascript text input fields that display instruction (placeholder) text

后端 未结 7 2075
抹茶落季
抹茶落季 2021-02-04 13:17

What\'s the best way to have a text input field that displays instructions of what to enter in that field in gray. When you focus on that input field, the gray instruction text

7条回答
  •  借酒劲吻你
    2021-02-04 13:26

    placeholder code - name

    name of the variable is,

    var name=$("#con-name");
    var nameval="Enter your name";
    
    
    
    name.val(nameval);
    name.focus(function(){  
         if (this.value == nameval ) { 
             this.value = ''; 
         }; 
        }).blur(function(){ 
             if (this.value == '') {    
             this.value = nameval;  
        };  
     });
     //submit to clear code
     $("#sub_con_page").click(function() {  
        if(name.val()==nameval) { 
           name.val(""); 
         } 
     });
    

    DEMO LINK

提交回复
热议问题