This is done automatically for every browser except Chrome.

I'm guessing I have to specifically target Chrome.

Any solutions?

If not with CSS, then with jQuery?

share
1  
how are you trying? – Toni Michel Caubet Mar 14 '12 at 17:39
    
cehck my edit then, it might help – Toni Michel Caubet Mar 14 '12 at 17:44
    
Opera is also another browser which removes placeholder on focus. – think123 Jul 27 '12 at 7:40
    
Firefox as of version 15 no longer removes the placeholder text until you start typing. I believe the same may be the case for IE10 but I don't have a way to verify that. – Rob Fletcher Sep 3 '12 at 17:19
    
I am concerned that nobody mentioned the fact that you shouldn't bother modifying native browser behaviors. I for one prefer that the placeholder remains present. It just helps me as an end user, and it's a feature that browsers are now starting to implement... probably because the disappear-on-focus behavior proved to be a usability problem. Let the browser be, please. – Ryan Wheale Jan 11 '13 at 17:38

21 Answers 21

up vote 141 down vote accepted

EDIT:

<input type="text" placeholder="enter your text" onfocus="this.placeholder = ''" onblur="this.placeholder = 'enter your text'" />
share
1  
This makes it go but when I click away from the field it remains blank. – LondonGuy Mar 14 '12 at 17:41
2  
@LondonGuy: Just edited my post, see if it works like you wanted. But Toni Michel Caubet solution is nicer – MatuDuke Mar 14 '12 at 17:45
1  
This was easy for me. Thanks it worked. – LondonGuy Mar 14 '12 at 18:20
9  
The problem here is this is obtrusive JavaScript. Toni Michel Caubet's solution is better. – Silkster Apr 11 '13 at 19:25
1  

Firefox 15 and IE 10+ also supports this now. To expand on Casey Chu's CSS solution:

input:focus::-webkit-input-placeholder { color:transparent; }
input:focus:-moz-placeholder { color:transparent; } /* FF 4-18 */
input:focus::-moz-placeholder { color:transparent; } /* FF 19+ */
input:focus:-ms-input-placeholder { color:transparent; } /* IE 10+ */
share
13  
Best answer I've seen, works wonderfully & without JS. – Simon East Jan 15 '13 at 4:06
    
Great answer! I don't see much sense in abandoning my old jQuery solution in favor of HTML5 and then go right ahead and add the JavaScript back in as a fix. This is just the solution I was looking for. – nienn Mar 26 '13 at 17:30
    
THis doesn't seem to be working for me in Firefox – Martin Hunt Apr 8 '13 at 17:43
    
@MartinHunt have you tried this on FF? input:focus::-moz-placeholder – Philip May 3 '13 at 9:50
15  
Apologies for dredging up an old thread, but just to make this more complete: input:focus:-moz-placeholder is for Firefox 18 and below, for 19 onwards you need to use: input:focus::-moz-placeholder (note the double colon). Ref: css-tricks.com/snippets/css/style-placeholder-text – Peter Lewis Jul 17 '13 at 8:56

Here is a CSS-only solution (for now, only works in WebKit):

input:focus::-webkit-input-placeholder {
    opacity: 0;
}
share
7  
Up voted for not using Javascript. – Rob Fletcher Sep 3 '12 at 14:05
1  
I like this answer, but the browser support for this just isn't there yet. – Jerry Oct 23 '12 at 15:09
    
Now that it's 2015, I think browsers can handle this... This was the simplest solution for me. – mcjoejoe0911 May 17 '15 at 4:12
1  
It's 2017 now, it should all work haha – JGallardo Jan 27 at 1:15

have you tried placeholder attr?

<input id ="myID" type="text" placeholder="enter your text " />

-EDIT-

I see, try this then:

$(function () {

    $('#myId').data('holder', $('#myId').attr('placeholder'));

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


});

Test: http://jsfiddle.net/mPLFf/4/

-EDIT-

Actually, since placeholder should be used to describe the value, not the name of the input. I suggest the following alternative

html:

<label class="overlabel"> 
    <span>First Name</span>
    <input name="first_name" type="text" />
</label>

javascript:

$('.overlabel').each(function () {
    var $this = $(this);
    var field = $this.find('[type=text], [type=file], [type=email], [type=password], textarea');
    var span = $(this).find('> span');
    var onBlur = function () {
        if ($.trim(field.val()) == '') {
            field.val('');
            span.fadeIn(100);
        } else {
            span.fadeTo(100, 0);
        }
    };
    field.focus(function () {
        span.fadeOut(100);
    }).blur(onBlur);
    onBlur();
});

css:

.overlabel {
  border: 0.1em solid;
  color: #aaa;
  position: relative;
  display: inline-block;
  vertical-align: middle;
  min-height: 2.2em;
}
.overlabel span {
  position: absolute;
  left: 0;
  top: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.overlabel span, .overlabel input {
  text-align: left;
  font-size: 1em;
  line-height: 2em;
  padding: 0 0.5em;
  margin: 0;
  background: transparent;
  -webkit-appearance: none; /* prevent ios styling */
  border-width: 0;
  width: 100%;
  outline: 0;
}

Test:

http://jsfiddle.net/kwynwrcf/

share
    
I already have the place holder text. That's not the problem. I would like the text to disappear when a user clicks on the field and not just when they start typing. – LondonGuy Mar 14 '12 at 17:39
1  
@LondonGuy check Edit – Toni Michel Caubet Mar 14 '12 at 17:55
1  
hey this is working fine :) – Suresh Pattu Aug 29 '12 at 6:10
    
I am glad it helps – Toni Michel Caubet Aug 29 '12 at 14:15
    
Nice to see the data attribute being used. But I would look at the CSS equivalent. When cached it will be a faster solution and can be global. The above needs the data attribute to be placed on every element needed. (answer below) – Neil Apr 30 '14 at 18:04

To augment @casey-chu's and pirate rob's answer, here's a more cross browser compatible way:

    /* WebKit browsers */
input:focus::-webkit-input-placeholder { color:transparent; }

    /* Mozilla Firefox 4 to 18 */
input:focus:-moz-placeholder { color:transparent; }

    /* Mozilla Firefox 19+ */
input:focus::-moz-placeholder { color:transparent; }

    /* Internet Explorer 10+ */
input:focus:-ms-input-placeholder { color:transparent; }
share
    
Exactly what i have written now (instead i used opacity:0;)! The only CSS solution in this thread with all possible browser supports! – Pixelkrieg Feb 23 '15 at 9:03

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.

share
    
makes sense, good point – Toni Michel Caubet Aug 27 '12 at 15:18
2  
This doesn't work if you have more than one field. Here is enhanced version of your code jsfiddle.net/6CzRq/64 – svlada Jan 29 '14 at 15:07

I like to package this up in the name space and run on elements with the "placeholder" attribute...

$("[placeholder]").togglePlaceholder();

$.fn.togglePlaceholder = function() {
    return this.each(function() {
        $(this)
        .data("holder", $(this).attr("placeholder"))
        .focusin(function(){
            $(this).attr('placeholder','');
        })
        .focusout(function(){
            $(this).attr('placeholder',$(this).data('holder'));
        });
    });
};
share
2  
Way more reusable :) – Toni Michel Caubet Oct 22 '12 at 11:55

To further refine Wallace Sidhrée's code sample:

$(function()
{  
      $('input').focusin(function()
      {
        input = $(this);
        input.data('place-holder-text', input.attr('placeholder'))
        input.attr('placeholder', '');
      });

      $('input').focusout(function()
      {
          input = $(this);
          input.attr('placeholder', input.data('place-holder-text'));
      });
})

This ensures that each input stores the correct placeholder text in the data attribute.

See a working example here in jsFiddle.

share
    
This is way better – JGallardo Jan 27 at 1:29

I like the css approach spiced with transitions. On Focus the placeholder fades out ;) Works also for textareas.

Thanks @Casey Chu for the great idea.

textarea::-webkit-input-placeholder, input::-webkit-input-placeholder { 
    color: #fff;
    opacity: 0.4;
    transition: opacity 0.5s;
    -webkit-transition: opacity 0.5s; 
}

textarea:focus::-webkit-input-placeholder, input:focus::-webkit-input-placeholder  { 
    opacity: 0;
}
share

Using SCSS along with http://bourbon.io/, this solution is simple, elegant, and works on all web browsers:

input:focus {
  @include placeholder() {
    color: transparent;
  }
}

Use Bourbon ! It's good for you !

share

Sometimes you need SPECIFICITY to make sure your styles are applied with strongest factor id Thanks for @Rob Fletcher for his great answer, in our company we have used

So please consider adding styles prefixed with the id of the app container

    #app input:focus::-webkit-input-placeholder, #app  textarea:focus::-webkit-input-placeholder {
        color: #FFFFFF;
    }

    #app input:focus:-moz-placeholder, #app textarea:focus:-moz-placeholder {
        color: #FFFFFF;
    }

share

This piece of CSS worked for me:

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

}
share
1  
That's A nice way of doing it without JQuery :) – tehX Oct 12 '13 at 22:11

Building on @Hexodus and @Casey Chu's answers, here is an updated and cross-browser solution that leverages CSS opacity and transitions to fade the placeholder text out. It works for any element that can use placeholders, including textarea and input tags.

::-webkit-input-placeholder { opacity:1; -webkit-transition: opacity 0.5s; transition: opacity 0.5s; }
:-moz-placeholder { opacity:1; -moz-transition: opacity 0.5s; transition: opacity 0.5s; } /* FF 4-18 */
::-moz-placeholder { opacity:1; -moz-transition: opacity 0.5s; transition: opacity 0.5s; } /* FF 19+ */
:-ms-input-placeholder { opacity:1;  -ms-transition: opacity 0.5s; transition: opacity 0.5s; } /* IE 10+ */

*:focus::-webkit-input-placeholder { opacity:0; }
*:focus:-moz-placeholder { opacity:0; } /* FF 4-18 */
*:focus::-moz-placeholder { opacity:0; } /* FF 19+ */
*:focus:-ms-input-placeholder { opacity:0; } /* IE 10+ */
share
1  
The best CSS solution! – Fatih SARI Jun 30 '16 at 19:47

HTML:

<input type="text" name="name" placeholder="enter your text" id="myInput" />

jQuery:

$('#myInput').focus(function(){
  $(this).attr('placeholder','');
});
$('#myInput').focusout(function(){
  $(this).attr('placeholder','enter your text');
});
share
1  
This only assumes one input. – nym Mar 24 '15 at 23:10

Demo is here: jsfiddle

Try this :

//auto-hide-placeholder-text-upon-focus
if(!$.browser.webkit){
$("input").each(
        function(){
            $(this).data('holder',$(this).attr('placeholder'));
            $(this).focusin(function(){
                $(this).attr('placeholder','');
            });
            $(this).focusout(function(){
                $(this).attr('placeholder',$(this).data('holder'));
            });

        });

}
share
    
use $("input[placeholder]") instead to only select fields that have a placeholder attribute. – Silkster Apr 11 '13 at 19:27
    
This is the best answer, simple, and deselects the others when out of focus – JGallardo Jan 27 at 1:13

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; }
share
$("input[placeholder]").focusin(function () {
    $(this).data('place-holder-text', $(this).attr('placeholder')).attr('placeholder', '');
})
.focusout(function () {
    $(this).attr('placeholder', $(this).data('place-holder-text'));
});
share
    
It will fail second time you use focusin.. – Toni Michel Caubet Jun 29 '16 at 7:04

For a pure CSS based solution:

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

Note: Not yet supported by all browser vendors.

Reference: Hide placeholder text on focus with CSS by Ilia Raiskin.

share
$("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");
    });
});
share

Besides all of above,I have two ideas.

You can add an element that imitates the palceholder.Then using javascript control the element showing and hiding.

But it is so complex,the other one is using the brother's selector of css.Just like this:

.placeholder { position: absolute; font-size: 14px; left: 40px; top: 11px; line-height: 1; pointer-events: none; }
.send-message input:focus + .placeholder { display: none; } 

23333,I have a poor English.Hope solve your problem.

share
    
Please check this URL it will be useful to lift your content quality up ; – Willie Cheng May 27 '16 at 3:51

try this function:

+It Hides The PlaceHolder On Focus And Returns It Back On Blur

+This function depends on the placeholder selector, first it selects the elements with the placeholder attribute, triggers a function on focusing and another one on blurring.

on focus : it adds an attribute "data-text" to the element which gets its value from the placeholder attribute then it removes the value of the placeholder attribute.

on blur : it returns back the placeholder value and removes it from the data-text attribute

<input type="text" placeholder="Username" />
$('[placeholder]').focus(function() {
    $(this).attr('data-text', $(this).attr('placeholder'));
    $(this).attr('placeholder', '');
  }).blur(function() {
      $(this).attr('placeholder', $(this).attr('data-text'));
      $(this).attr('data-text', '');
  });
});

you can follow me very well if you look what's happening behind the scenes by inspecting the input element

share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.