views:

109

answers:

3

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.

+1  A: 

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. http://stackoverflow.com/questions/1657170/detecting-support-for-specific-html-5-features-via-jquery/1657182#1657182

Marko
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
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
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
+1  A: 

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.

David Dorward
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
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
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).
David Dorward
A: 

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
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.
David Dorward