views:

61

answers:

5

Many pages have a search box to them which will typically have the overlayed text "Search" which disappears when one focuses the element and reappears when focus is lost. I'm curious to know what people recommend as the best strategy for this.

The strategy I've employed is to use the focus/blur events of the input element and test the content to determine if the value should be changed. In my following example I use jQuery. Take an example where we have an input element with an id of quick-search, when empty I show the text "Search" when focussed I remove the text and update a style,

$(function() {
  $("#quick-search").focus(function() {
    if (this.value === "Search") {
      $(this).removeClass("quick-search-not-focussed");
      this.value = "";
    }
  }).blur(function() {
    if (this.value === "") {
      $(this).addClass("quick-search-not-focussed");
      this.value = "Search";
    }
  });
})

My quick-search-not-focussed class looks as follows:

.quick-search-not-focussed { color: #bbb; }

This works well for me as search boxes can only really be submitted on enter as there is no button, however some scenarios require more input elements with input text overlayed, what are the alternative tricks/techniques you've used? Personally I don't like the use of images in this approach.

+4  A: 

jQuery placeholder

Actually, there are quite a few similar plugins. The one I linked is good enough for me.

Mauricio Scheffer
and if the rest of the page has no use for jQuery, then what's the point in sending all that extra content to the page?
rockinthesixstring
@rockinthesixstring: then use your solution, by all means.
Mauricio Scheffer
not arguing. Just pointing out the facts. your solution is a good one... just overkill if it's the only JS content on the page.
rockinthesixstring
@rockinthesixstring: No arguing either. I'd honestly use your answer in that case. The OP mentioned he's already using jQuery though.
Mauricio Scheffer
It's funny how we can get so caught up in defending our answers when really both answers are equally accurate. A simple question can become argumentative simply on preference. I suppose we should just chalk it up to "6 of one, half a dozen of the other"
rockinthesixstring
I really like this approach, thanks for this. It makes it simple by holding the placeholder attribute.
Brett Ryan
btw, jQuery usage is fine as it's being used extensively within the application already.
Brett Ryan
+1  A: 

I've used the following in my code.

<input id="searchBox" 
       class="search-gray" 
       name="q" tabindex="1" 
       onblur=" if (this.value==''){this.value = 'search...'; this.className = 'search-gray'}" 
       onfocus=" this.className = ''; if (this.value=='search...') {this.value = ''}" 
       type="text" 
       value="search...">

<style>
    .search-gray{color:#c5c5c5;}
</style>

EDIT
Here's what StackOverflow uses for their search box

 <input name="q" 
       class="textbox" 
       tabindex="1" 
       onfocus="if (this.value=='search') this.value = ''" 
       type="text" 
       maxlength="80" 
       size="28" 
       value="search">
rockinthesixstring
that's a lot to send down the pipe for a single text field.
tster
it's a similar method to the one used here on SO
rockinthesixstring
Obtrusive JS = bad.
Josh Leitzel
Tell that to @Jeff Atwood.
rockinthesixstring
I guess even SO has its design flaws. ;)
Josh Leitzel
I think it's funny how many people use the "Jeff Atwood does it so it must be right" argument. BTW, this is fine if it's 1 search box. it becomes more annoying when you are doing this to decorate and entire form.
tster
Also, the SO code is quite a bit shorter than yours (granted, less functionality too)
tster
I just added the CSS and the ability to return it to it's original state. Also, I'm not saying that if JA uses it, it must be right... I'm saying if it's good enough for a website with far more traffic than any one of us is going to build... then it's good enough for me too. Since the OP was stating that he was looking for the ability to overlay a "search", I didn't bother going into heavy `<script>` tags for a single input.
rockinthesixstring
BTW if you compare @Josh's answer (and how much he's sending down the pipe) vs mine, I'd say that mine is far less. Especially since you don't need to include the jQuery library. If the rest of the page has no jQuery "stuff", then whats the point of adding all that extra crap for a single input?
rockinthesixstring
And if the rest of the page has no use for jQuery, then this "is" the better solution for a single input form.
rockinthesixstring
The jquery code in josh's answer could be put in a seperate javascript file (combined with all the other .js includes of course) and cached in the browser resulting in no extra data 99% of the time. (NOTE: this depends on what kind of app you are making. I do mostly internal stuff, so the cache is always loaded)
tster
@rockinthesixstring, since this is a site for professional developers I wouldn't assume no one here works on sites with more traffic than this one ;)
tster
It's funny how we can get so caught up in defending our answers when really both answers are equally accurate. A simple question can become argumentative simply on preference. I suppose we should just chalk it up to "6 of one, half a dozen of the other"
rockinthesixstring
@rock: I'm sorry if I offended you, I really only said that obtrusive JS is bad (which it is). I don't doubt that your answer is an answer to OP's question. I just think standards are important, and sites large and small should adhere to them because in the end they make your life easier. And FWIW the OP used jQuery himself in his question so I don't think it was unreasonable for me to use it in my answer.
Josh Leitzel
I like this approach, nice and simple for small pages. However, what if the input element had other classes applied either by some other form of JS, this would wipe those out, of course this could be changed to a regex to overcome the problem :)
Brett Ryan
+1  A: 

I usually do something similar to this:

<input type="text" name="email" data-original="Email address" value="Email address" />
<input type="text" name="username" data-original="Username" value="Username" />

<script type="text/javascript">
    $(document).ready(function(){
        $('input[data-original]').each(function(){
            var input = $(this);
            var original = input.attr('data-original');

            input.focus(function(){
                if (input.val() == original)
                    input.addClass('focused').val('');
            });
            input.blur(function(){
                if (input.val() == '')
                    input.removeClass('focused').val(original);
            });
        });
    });
</script>

This is the same as your approach except with the advantage that you don't have to keep repeating your code for each field; instead you can just specify data-original on your fields and they will be good to go.

Josh Leitzel
and if the rest of the page has no use for jQuery, then what's the point in sending all that extra content to the page?
rockinthesixstring
The OP's code uses jQuery.
Josh Leitzel
Great approach, I do like this method as it's fairly simple, I did mark Mauricio Scheffer as answered because I like the fact of relying on a library :)
Brett Ryan
A: 

FWIW, Webkit browsers directly support a placeholder attribute on <input> tags:

​<input placeholder='enter something' />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
Scott Evernden
yes it works for browsers that support webkit, but what about the browsers that don't?
rockinthesixstring
I suppose in cases where the placeholder attribute is not supported, one could use this jQuery plugin. - http://plugins.jquery.com/project/html5-placeholder
rockinthesixstring
Good suggestion, however I don't and either should anyone else target a specific browser, with the exception that I won't support a browser older than 3 years.
Brett Ryan
+1  A: 

Have a look at this one: http://fuelyourcoding.com/scripts/infield/

Its different from the others here in that its a bit more usable. The label fades out on field focus and only disappears when you start typing.

and its unobtrusive.

Moin Zaman
That's actually a pretty neat plugin, I like it. I haven't tried the plugin, however would the tricky bit be on slow loading pages where "ready" hasn't fired cause pages to load with the label visible and then "move" into the input element? I'm just assuming this is what might happen, however this could be tackled I assume by setting the elements visibility before and after the call.
Brett Ryan