views:

80

answers:

3

You see these text input boxes from time to time on the web: a grey label is shown inside the box, but once you type there, the grey text disappears. This page even has one: the "Title" field behaves exactly like that.

So, questions:

  1. Is there a standard term for this? I'm really struggling to find anything on google

  2. Can it be done with just CSS?

  3. Failing that, can it be done with localised JavaScript? (ie, code just within the tag, not in the HTML header).

+2  A: 

I don't know of a way to do it with CSS. But looking at the page source, SO is doing it thusly-wise:

<input name="q" class="textbox" tabindex="1" 
       onfocus="if (this.value=='search') this.value = ''" 
       type="text" maxlength="80" size="28" value="search">

The middle line starting with "onfocus" is where the work is happening. When the text field gets focus, it checks to see if the default value ("search") is there. If so, it sets the value to '' (empty string). Otherwise, the text is unaffected.

One potential issue here is that if someone is trying to search for the word "search", then clicks outside of the box and clicks back in again, the text gets reset. Not a huge issue, but something to keep in mind as you're working.

AaronM
I'm surprised SO actually does it this way. A better method would be to use a flag instead of directly comparing the text.
casablanca
@casablanca There are some questionable front end things in the SO source - probably done for speed of development over long term maintainability.
alex
@casablanca - SO doesn't care for standards or best practices. Tables are used everywhere, even in places where lists would be better. Inline Javascript too, even when it's trivial to do otherwise.
Yi Jiang
I would guess that they've been busy with other things. those boys don't exactly sit still...
STW
I agree. SO can have some pretty weird code. Based off of the code, if you are searching for "search" and you accidentally click out of the textbox, next time you click it, it will be blank.
Kranu
@Yi Jiang - It may not be as un-DRY as it seems. It's possible they have something on the server-side that generates search boxes, and it's easy to change a line or two of their backend code to update every search box at once.Who knows? Unless Jeff or somebody comes out and tells us, I'm just speculating.
AaronM
@Aaron - You might be right about the search box, but tables for list... come on, surely there's no excuse for using that! The code's almost the same, but with lists you get both better semantics and less markup.
Yi Jiang
My guess is that they don't really care, the ad-revenue is making them rich. If it's working and it's not less-secure, why invest time, money, or stress? Also, as stated before SO programmers are not the best - they've had bigger problems injection hacks.
vol7ron
A: 
  1. I call them input labels.. don't know if it is a standard
  2. Nope - you could if they were allowed child elements like so

    input { position: relative; }

    input label { position: absolute; top: 0; left: 0; }

    input:focus label { display: none; }

    But you can't have children of inputs (AFAIK), so it won't work.

  3. Yes, see the link above (using jQuery, but trivially done without)

alex
+2  A: 

If you want this with HTML + CSS only, you can try using the new HTML5 placeholder input attribute. Unfortunately this attribute is not widely supported, so try using a script such as Modernizr to detect browser support for this feature for a feature such as this.

I would not recommend using inline Javascript for this - it's bad practice and goes against the principle of separation of content and behavior. Using jQuery, for instance, something like this will work:

var input = $('input[name="search"]');
var placeHolder = input.attr('placeholder');

input.val(placeHolder);

input.focus(function(){
    this.value = '';
}).blur(function(){
    if(this.value === '') this.value = placeHolder;
});
Yi Jiang