How do I auto-hide placeholder text upon focus using css or jquery?

后端 未结 26 1751
长情又很酷
长情又很酷 2020-11-28 00:39

This is done automatically for every browser except Chrome.

I\'m guessing I have to specifically target Chrome.

Any solutio

相关标签:
26条回答
  • 2020-11-28 01:14

    for input

    input:focus::-webkit-input-placeholder { color:transparent; }
    input:focus:-moz-placeholder { color:transparent; }
    

    for textarea

    textarea:focus::-webkit-input-placeholder { color:transparent; }
    textarea:focus:-moz-placeholder { color:transparent; }
    
    0 讨论(0)
  • 2020-11-28 01:14
    $("input[placeholder]").each(function () {
        $(this).attr("data-placeholder", this.placeholder);
    
        $(this).bind("focus", function () {
            this.placeholder = '';
        });
        $(this).bind("blur", function () {
            this.placeholder = $(this).attr("data-placeholder");
        });
    });
    
    0 讨论(0)
  • 2020-11-28 01:17

    Toni's answer is good, but I'd rather drop the ID and explicitly use input, that way all inputs with placeholder get the behavior:

    <input type="text" placeholder="your text" />
    

    Note that $(function(){ }); is the shorthand for $(document).ready(function(){ });:

    $(function(){
        $('input').data('holder',$('input').attr('placeholder'));
        $('input').focusin(function(){
            $(this).attr('placeholder','');
        });
        $('input').focusout(function(){
            $(this).attr('placeholder',$(this).data('holder'));
        });
    })
    

    Demo.

    0 讨论(0)
  • 2020-11-28 01:19

    The same thing i have applied in angular 5.

    i created a new string for storing placeholder

    newPlaceholder:string;
    

    then i have used focus and blur functions on input box(i am using prime ng autocomplete).

    Above placeholder is being set from typescript

    Two functions i am using -

    /* Event fired on focus to textbox*/
    Focus(data) {
        this.newPlaceholder = data.target.placeholder;
        this.placeholder = '';
    }
    /* Event fired on mouse out*/
    Blur(data) {
        this.placeholder = this.newPlaceholder;
    }
    
    0 讨论(0)
  • 2020-11-28 01:19

    No need to use any CSS or JQuery. You can do it right from the HTML input tag.

    For example, In below email box, the placeholder text will disappear after clicking inside and the text will appear again if clicked outside.

    <input type="email" placeholder="Type your email here..." onfocus="this.placeholder=''" onblur="this.placeholder='Type your email here...'">
    
    0 讨论(0)
  • 2020-11-28 01:21

    2018 > JQUERY v.3.3 SOLUTION: Working globaly for all input, textarea with placeholder.

     $(function(){
         $('input, textarea').on('focus', function(){
            if($(this).attr('placeholder')){
               window.oldph = $(this).attr('placeholder');
                $(this).attr('placeholder', ' ');
            };
         });
    
         $('input, textarea').on('blur', function(){
           if($(this).attr('placeholder')){
                $(this).attr('placeholder', window.oldph);
             };
         }); 
    });
    
    0 讨论(0)
提交回复
热议问题