HTML forms and onclick/onfocus

Update: This post is ancient, with CSS3 and HTML5 there are much nicer ways to achieve similar things.

When you use HTML forms it is often desirable to pre-fill some input fields. Your idea could be that you want to give your visitors some clue to what they're supposed to fill in.

This looks nice, for example:

When the user clicks into the field you'll want to remove the clue. The obvious way to do this is to add an onclick JavaScript command:

 
<form>
	<input onclick="this.value=''" type="text" value="Your email">
</form>

The user can still access the input field through other means than clicking. He can use the tab key for example. That's why usually the onfocus event is more useful:

 
<form>
	<input onfocus="this.value=''" type="text" value="Your email">
</form>

This seems to work nicely. However there's still a problem. When a user fills in his email address, reads a little more on your site and clicks the input field again, his address will be removed again! So to make sure this doesn't happen we add a little check for the content of the field:

 
<form>
	<input type="text" value="Your email" onfocus="if (this.value == 'Your email') {this.value=''}">
</form>

And here it is:

Restore default value if nothing is entered

Ok, now this is working. However, when your visitor decides not to input any data, the field stays empty. If you have no label for the input he or she might forget what the field was for. We fix this by restoring the default value when the input field is left:

 
<form>
	<input type="text" value="Your email" onblur="if(this.value == '') { this.value='Your email'}" onfocus="if (this.value == 'Your email') {this.value=''}">
</form>

Thanks to Ray for his example linked in the comments.

Select onclick

But sometimes this isn't the best solution. It could be more desirable to highlight the content of the input field if you're offering some kind of search. The user may not want to start a new search but to modify the existing one. By highlighting the input field the user can choose to either empy it by starting to type or to modify it by using the arrow keys.

 
	<form>
		<input onclick="this.select()" type="text" value="Your previous searcj">
	</form>

Using onclick here makes sense as the content is selected anyway if you use the tab key to access it. Webkit-based browsers also don't handle onfocus nicely.

Dynamic input color

This is a little advanced. Let's assume you want the default value to be in a different color and change it onclick. This is one working solution:

 
<form>
	<input style="color: #f00" type="text" value="Your email" onblur="if(this.value == '') { this.style.color='#f00'; this.value='Your email'}" onfocus="if (this.value == 'Your email') {this.style.color='#0f0'; this.value=''}">
</form>

Keep in mind that this might create problems when you reload the page. Some browsers will remember the value of the input field, and the color will be different even though the field does not contain the default value. Fixing that is beyond the scope of this post. See onload and domready events.

Published on Sept. 20, 2008 at 10:34 a.m. by Nicolas and tagged usability, HTML, Webdesign, development, JavaScript, onclick, onfocus. You can follow the discussion with the comment feed for this post.

31 comments

  • avatar
    William Uzelac wrote this comment on Nov. 11, 2008, 6:18 a.m.
    That is great. Now additional question if I may. If I want "Your email" value to return back on page load, what do I change? Thanks, William
    Reply to this comment
    • avatar
      nicolas wrote this comment on Nov. 11, 2008, 8:56 a.m.
      I guess you could use an onload or onunload event. Something like <pre>window.onload=function()</pre> Not sure why you would want to do that though.
      Reply to this comment
  • avatar
    Ray wrote this comment on Jan. 23, 2009, 5:49 p.m.
    i've got it all set, but i want the default text to be grey, and then when the user types in the input, I want the new text top be black.... Can you help me? here's my form <strong>Edit: remove 404</strong>
    Reply to this comment
    • avatar
      nicolas wrote this comment on Jan. 23, 2009, 6:18 p.m.
      Another note, if you reload the page the input value is still what the user entered, but the text would remain grey. Fixing that would probably be a little tricky, you'd need an onload or some ondomready event that checks the content of all inputs and changes the color. But that's only required if you want to make a perfect form.
      Reply to this comment
      • avatar
        Ray wrote this comment on Jan. 23, 2009, 6:40 p.m.
        That's strange.. When I reload the page it resets to the default.
        Reply to this comment
        • avatar
          Ray wrote this comment on Jan. 23, 2009, 6:52 p.m.
          Ok i now see in Firefox that reloading the page leaves the user entered text.... This is not the case in Safari...
          Reply to this comment
          • avatar
            nicolas wrote this comment on Jan. 23, 2009, 7:06 p.m.
            Yeah. I have updated the post with a color switching example and added your onblur technique. That color switchin and page reloading thing needs to be tested in detail...
            Reply to this comment
            • avatar
              Ray wrote this comment on Jan. 23, 2009, 7:32 p.m.
              Thank Nicolas- After looking at your code I notice you had put the this.style.color=’#000′ in a different spot then you had mentioned before.... It's all working good now- Now if only I could get the progressive upload status bar working... Any ideas?
              Reply to this comment
        • avatar
          nicolas wrote this comment on Jan. 23, 2009, 6:42 p.m.
          onfocus="this.style.color='#000'; if (this.value == 'Who is the Skater?') {this.value=''}"
          Reply to this comment
          • avatar
            Ray wrote this comment on Jan. 23, 2009, 6:50 p.m.
            It's not working for me. I'm on a Mac checking with safari 3 and firefox 2. When I add the this.style.color=’#000′; snippet, not only does it not change the font to black-- but it also removes the functionality of the "this.value" ... AIM=onepercentposse
            Reply to this comment
      • avatar
        Ray wrote this comment on Jan. 23, 2009, 6:32 p.m.
        I cannot seem to get that this.style.color=’#000′; to work? original input can you show me exactly where to put it - thank so much
        Reply to this comment
      • avatar
        dredz wrote this comment on Feb. 24, 2012, 8:44 p.m.
        hi im having problem in my color because first i want to turned it to gray then if the user wants to input something on the input box it will turn to color black but in my case it still in color gray pls help me

        heres the code im using

        <input style="color: #C0C0C0" onclick="this.value=''" type="text" name="fname" value="First Name" onblur="if(this.value == '') { this.value='First Name'}" onfocus="this.style.color='#000'; if (this.value == 'First Name') {this.value=''}"">
        Reply to this comment
    • avatar
      nicolas wrote this comment on Jan. 23, 2009, 6:13 p.m.
      You can add this.style.color='#000'; before the if(). I like how you reset the input value to the default if nothing is entered, I think I will add that to my post. That would have solved William's problem above too. And I'll add the color change too. Good comment :-)
      Reply to this comment
  • avatar
    pesura wrote this comment on Sept. 22, 2009, 2 p.m.
    oh thats great, thanks so much. i've just found i had been looking for :)
    Reply to this comment
  • avatar
    Leyton Jay wrote this comment on Oct. 13, 2009, 12:33 p.m.
    Thanks for these javascript tips Nicolas, they're really handy! I know no javascript what-so-ever despite being a bit of an expert in XHTML and CSS. I've decided to learn some, so this is a nice starting point, cheers!
    Reply to this comment
  • avatar
    Oli wrote this comment on Nov. 26, 2009, 5:50 p.m.
    Hi, Great article. However, when this is used with a javascript validator the validation script is ignoring the fields as they are already filled in. Is there a simple fix to this? Cheers
    Reply to this comment
  • avatar
    ed wrote this comment on March 1, 2010, 6:58 p.m.
    thank you! (city field on my form is color changing on entry now!)
    Reply to this comment
  • avatar
    snosie wrote this comment on March 12, 2010, 12:02 p.m.
    Thanks for the tips Could you also post a solution for 'textarea'? for some reason I can't get it to work. Thanks
    Reply to this comment
    • avatar
      Nicolas wrote this comment on March 13, 2010, 12:57 p.m.
      Hello snosie, for textareas you'll need something like <pre> &lt;textarea onfocus="this.innerHTML='bar';" &gt; foo &lt;/textarea&gt; </pre> Please test it across all browsers though, it's just a quick guess.
      Reply to this comment
  • avatar
    Edie wrote this comment on March 29, 2010, 11:55 a.m.
    Big thanks Bro :)
    Reply to this comment
  • avatar
    Brandon Stewart wrote this comment on April 19, 2010, 9:44 p.m.
    Thank you Nicolas, for the clear and concise tutorial.
    Reply to this comment
  • avatar
    Sv wrote this comment on July 2, 2010, 10:03 a.m.
    It's useful for me, thanks.
    Reply to this comment
  • avatar
    Velimir wrote this comment on Aug. 18, 2011, 5:13 p.m.
    Hi,

    thank you on post but i have problem in IE.
    How to get "Restore default value if nothing is entered" in in IE without "Allow blocked content" when IE restrict script running? Do i need some more script?

    Thank you...
    Reply to this comment
  • avatar
    Alan Tomkins wrote this comment on Jan. 8, 2012, 7:53 p.m.
    Thanks Nick, Great article with useful advice. It's helped me a lot as I needed text inside fielkds due to limited space for teh form.
    Top job,
    Alan
    Reply to this comment
  • avatar
    Venkat wrote this comment on Jan. 10, 2012, 1:28 p.m.
    HI , I have searched many site, but not able to get this kind of explanation really super thank you for your great UI.

    Venkat
    Reply to this comment
  • avatar
    Wayne Gomez wrote this comment on March 6, 2012, 4:55 p.m.
    How could you decrease li and width when you click on a searchbox like this one? The searchbox is also within li.


    <li>Home</li>
    <li>Contact</li>

    <li class="navbar_right">
    <input id="search_box" type="text" value="Search" onblur="if(this.value == '')
    { this.value='Search'}" onfocus="if (this.value == 'Search') {this.value=''}, ">
    </li>
    Reply to this comment
    • avatar
      Nicolas Kuttler wrote this comment on March 7, 2012, 8:43 p.m.
      Well there are various techniques to do this. You can use some effects library, simply set the css property, update the class, etc.
      Reply to this comment
  • avatar
    SF wrote this comment on April 3, 2012, 9:37 p.m.
    Thanks a ton, a nice simple tutorial.
    Reply to this comment
  • avatar
    merih wrote this comment on April 20, 2012, 5:58 p.m.
    how to apply this javascript into password textboxes ?? because password textbox does not write "Your Email" deafult text, instead it shows ******* . So, what is the solution ??
    Reply to this comment
    • avatar
      Nicolas Kuttler wrote this comment on May 1, 2012, 5:31 p.m.
      I guess the new placeholder could work for that, see

      http://www.whatwg.org/specs/web-apps/current-work/multipage/common-input-element-attributes.html#attr-input-placeholder
      Reply to this comment

Start a new thread

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.