18

This works in Chrome and any other browser that supports placeholder text in HTML5

<input id="name" name="name"  type="text" placeholder="Please enter your name..." required /> <br />

But, it doesn't work in 3.5 and earlier of Firefox, and obviously IE8, and possibly other browsers.

How do I achieve the same thing (preferably in HTML/CSS - if not I am open to suggestions), to support all the older browsers? If not every single browser, at least Firefox and IE.

Safari and Chrome already support it (or the latest versions anyway).

Thanks.

Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158
marcamillion
  • 32,933
  • 55
  • 189
  • 380

10 Answers10

19

One day I'll get around to properly documenting this, but see this example: http://dorward.me.uk/tmp/label-work/example.html

In short — position a <label> under a transparent <input> using <div> to provide background colour and borders.

Then use JS to determine if the label should be visible or not based on focusing.

Apply different styles when JS is not available to position the label beside the element instead.

Unlike using the value, this doesn't render the content inaccessible to devices which only display the focused content (e.g. screen readers), and also works for inputs of the password type.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 2
    This is a brilliant solution. I just checked that page in all the browsers I am looking for, and it works. I haven't started digging down into it yet, or even tried applying it...but it seems that you sir...have provided a credible answer to my question. Thanks. – marcamillion Aug 07 '10 at 10:20
  • Tried this...the issue I am having is that it requires a static positioned textbox, using the top and left attributes in CSS. My textboxes were dynamically positioned on the screen. So it doesn't quite work for what I want to do. I did find a plugin that works nicely though :) – marcamillion Aug 08 '10 at 09:41
  • The input element itself has to be absolutely positioned (which is what I think you mean when you say 'static' (which means the opposite!) — but it is positioned within the context of a div which is relatively positioned. Relative positioning keeps the element in *normal flow* (and in the absence of top, left, right or bottom does layout exactly like position: static. You can change the div to be position absolute or fixed if you like (it will still act as the containing block). – Quentin Aug 08 '10 at 09:46
  • Wow that's genius! I can't believe I didn't think about checking if the inputs are transparent. No more checking to see that the placeholder text isn't there anymore. – OzzyTheGiant Aug 04 '16 at 14:15
15

I use this one: https://github.com/danbentley/placeholder
Lightweight and simple jQuery plugin.

Florian
  • 790
  • 1
  • 13
  • 23
  • 2
    It does work, just tested it. I imagine you downloaded the sample and opened it from your file system. IE will not run JavaScript if you do this. You have to host it in IIS. – Juliano Dec 13 '12 at 16:36
7

Here is the simplest solution that I found working everywhere:

<input id="search" 
   name="search" 
   onblur="if (this.value == '') {this.value = 'PLACEHOLDER';}" 
   onfocus="if (this.value == 'PLACEHOLDER') {this.value = '';}"
/>

Replace PLACEHOLDER with your own.

At the moment, FF3 does not yet support the "placeholder" attribute of the "input" element. FF4, Opera11 and Chrome8 support it partially, i.e. they render the placeholder text in the field, but do not delete it when the user focuses in the field, which is worse that not supporting it at all.

Marios Zindilis
  • 201
  • 3
  • 2
  • This submits the placeholder value as the form value, which makes it pretty useless. – Peter Davis Mar 10 '12 at 03:13
  • Two things wrong with this: it fills the form with actual values, which is not optimal if you have multiple fields in your form, it also only does so after clicking on the elements, which is not the intention of the OP. – shadowmanwkp Feb 25 '14 at 13:40
4

I use the following snippet that I wrote with jQuery. Just add a class of textbox-auto-clear to any textbox on the page and you should be good to go.

<input type="text" value="Please enter your name" class="textbox-auto-clear" />

$(".textbox-auto-clear").each(function(){
    var origValue = $(this).val(); // Store the original value
    $(this).focus(function(){
        if($(this).val() == origValue) {
            $(this).val('');
        }
    });
    $(this).blur(function(){
        if($(this).val() == '') {
            $(this).val(origValue);
        }
    });
});

I assume that you want to keep using the placeholder attribute for HTML5 browsers, in which case you'd have to do some browser detection and only apply the jQuery solution to browsers that don't support it.

Better yet, you can us the Modernizer library, as outlined in this answer. Detecting support for specific HTML 5 features via jQuery

Community
  • 1
  • 1
Marko
  • 71,361
  • 28
  • 124
  • 158
  • This is good when the field comes in focus. How about when it is just loaded. I would like it to put the text in the field before the user even clicks on it - just like HTML5 does with the placeholder attribute. Any thoughts on how to do this? – marcamillion Aug 07 '10 at 07:52
  • Also, if I have a number of input fields, all with unique IDs, how do I apply a different message to each one? This one applies it to all the fields. I mean, I could just duplicate this function for each unique ID, but that doesn't DRY-like. – marcamillion Aug 07 '10 at 07:54
  • Ahh yes, I forgot to mention this relies on the value attribute of the textbox. If you AREN'T using the placeholder text and go with the solution above, simply set the value attribute on each of the fields (i.e. Please enter your name) and just add a class of textbox-auto-clear. If you want to use the placeholder attribute in conjunction with the solution above, you'd have to set the values of each textbox on page load using javascript. Not a pretty solution though, I'd suggest using one or the other. – Marko Aug 07 '10 at 08:04
4

Here is a MooTools Plugin, that brings the placeholder to browsers that don't support it yet:

http://mootools.net/forge/p/form_placeholder

Matt
  • 1,143
  • 1
  • 11
  • 19
3

I use this one: https://github.com/Jayphen/placeholder This lightweight and simple jQuery plugin is a fork of danbentley/placeholder.

Advantage: it adds a class "placeholder" to input fields that are temporarily filled. Css ".placeholder {color:silver}" make the polyfill text look like a placeholder instead of regular input text.

Disadvantage: It doesn't polyfill the placeholder of a password field.

Pro Backup
  • 729
  • 14
  • 34
2

By the way...if anyone is interested...I found a nice elegant solution that is a jQuery plugin that is SOOO nice.

It literally is one line of jQuery, a minified js plugin, along with a simple class name on the input.

http://labs.thesedays.com/projects/jquery/clearfield/

It's the most beautiful thing I have discovered, next to 'Placeholder' in html.

marcamillion
  • 32,933
  • 55
  • 189
  • 380
  • This abuses the value to provide information to users. If JS isn't available users have to delete the value manually. If JS is available and the user depends on a screen reader then the information will be removed as soon as the focus hits the element — so it never gets read out. – Quentin Aug 08 '10 at 09:47
  • I don't like this solution. I'm using Chrome 12 and trying it out, and the placeholder text gets stuck as the value when you submit forms. Then I end up with a bunch of comments with the text "comments..." ;) – Ian Hunter Jun 10 '11 at 23:34
2

The trick is to use javascript functions onBlur() and onFocus().

Here is the code that worked for me:

<script language="javascript" type="text/javascript" >

    var hint_color = "grey", field_color = null;

    var hinted = true;

    function hint() { // set the default text

        document.getElementById('mce-EMAIL').style.color = hint_color;
        document.getElementById('mce-EMAIL').value = "<?php echo SUBSCRIPTION_HINT; ?>";

        hinted = true;

    }

    function hintIfEmpty() { // set the default text, only if the field is empty

        if (document.getElementById('mce-EMAIL').value == '') hint();

    }

    function removeHint() {

        if (hinted) {

            document.getElementById('mce-EMAIL').style.color = field_color;
            document.getElementById('mce-EMAIL').value = "";

            hinted = false;

        }

    }

    function send() {

        document.getElementById('subscription_form').submit();
        hint();

    }

</script>

<div style="position:absolute; display: block; top:10; left:10; ">
<form id="subscription_form" action="<?php echo SUBSCRIPTION_LINK; ?>" method="post" target="_blank">

    <input type="email" value="" name="EMAIL" class="email" id="mce-EMAIL" style="width: 122px;" onBlur="hintIfEmpty();" onFocus="removeHint();" required>
    <a href="javascript:send()"><font style="position: relative; top:-1px;"><b>ok</b></font></a>

</form>
</div>

<script language="javascript" type="text/javascript" >

    field_color = document.getElementById('mce-EMAIL').style.color;
    hint();

</script>

SUBSCRIPTION_HINT (i.e.: "your e-mail" ) and SUBSCRIPTION_LINK (i.e.: the value of the 'action' tag in your EN mailchimp embed code...) are PHP constants used for localization.

Gilbou
  • 5,244
  • 6
  • 24
  • 27
1

For "placeholder" work in Firefox just add the attribute

::-moz-placeholder

in CSS tags.

Nicholas
  • 11
  • 1
  • Its working for me in firefox 46.0.1 without doing any css change. http://www.thesstech.com/tryme?filename=placeholder – Sohail Arif May 31 '16 at 19:55
0

Works for me, change your CSS to

::-webkit-input-placeholder {
  color: #999;
}
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Tamilselvan K
  • 1,133
  • 11
  • 10