views:

144

answers:

5

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 disappears and your input text appears black. If you erase your input text and focus away from that input, then the instruction gray text reappears.

I've tried implementing this with jQuery, but I get all kinds of weird issues with different browsers. Sometimes when you go back a screen, the instruction text becomes black and no longer disappears, issues like that.

Is there a standard javascript library for this? I can't find any!

Oh, and the instruction text can't be an image because it has to be available in different languages depending on the browser's language preference.

A: 
<input type="text" name="myText" value="Enter Name" style="color:gray" onfocus="this.value=''">
ppshein
This misses most of the requirements in the question and has accessibility problems.
David Dorward
This won't work. Because after you focus on this field, the instruction text disappear forever
Bang Dao
Sorry, guys. I though the wrong way.
ppshein
A: 

Make the input transparent and put the text behind it. Check the value onload, onblur to decide if the text should be visible or not. Make it invisible onfocus.

For example: http://dorward.me.uk/tmp/label-work/example.html (you just need to style the label and input with different foreground colours)

David Dorward
I love this idea. The instruction text is also indexable too, which just makes sense. I went with the jQuery watermark plugin though because it was so simple and made the html so clean. For all I know, they implemented this with the same mechanism..
at
+1  A: 

You can use watermark plugin in jquery.use this link.

example

 $(this).Watermark("your instructions");
Amit
This is perfect, thanks!
at
A: 

html

<input type="text" id="user" class="textbox default" value="Enter login name">
<input type="password" id="pass" class="textbox">

jQuery

$(function(){
  $('.textbox').focus(function(){
    if (this.value == this.defaultValue) {
        this.value = '';
        $(this).removeClass('default');
    };
  }).blur(function(){
    if (this.value == '') {
        this.value = this.defaultValue;
        $(this).addClass('default');
    };
  });​
});

demo

Reigel
A: 

Look at this one: http://fuelyourcoding.com/scripts/infield/

Moin Zaman
love the fading! maybe I'll look into this later
at