Tuesday 07 February 2012

Clear Input Field Value When Selected

iStock_000016074597XSmall

Often when creating forms on a website it is helpful to place descriptive text with some of the form fields to help the user understand what they should type in the box. If this descriptive text is not cleared automatically when the user begins to type, sometimes this will hinder them more than it helps.

Using a simple JavaScript function you can automatically clear the example values from the input box when the user clicks into the field.

For an example of how this works, look at my newsletter fields to the right.

This technique can easily be used to clear the contents of any input box.

All you have to do, is add a small bit of JavaScript to your input like this:

<input value="Your Comment" onclick="this.value=''" type="text">

There are a lot of other ways to do this… Post yours below.

About the author

Founder of Eos Designs and co-founder of iPrintEasy and a number of other web businesses - an entrepreneur with over 10 years of experience in Internet Marketing - Zach understands what it takes to be successful online and specializes in the development of advertising campaigns and marketing plans for start-ups.
  • anon

    Brrrrilliant!!

    thanks, this is exactly what i wanted ;)

  • http://www.jordanmooredesign.com Jordan Moore

    Perfect! Thanks mate!

  • http://buildinganonline.com/blog/ Michael

    Thank you :)

    This little script solved a long boring problem, I’ve had.
    The working script can now also be seen at my site.

    You sure provided something of value.

    Michael’s last blog post… It’s Not Safe

    • zacheos

      You’re welcome ;)

  • http://www.buildinganonline.com Michael

    How do I make the value return to the input field, when clicking somewhere else, if I didn’t enter any text(like this: http://buildinganonline.com/contact)?

  • http://www.design19.ro Cluj

    Thnks for this.

  • Luna

    Thanks for this tip! ;) Bookmarked this page. :)

  • http://www.dlocc.com Devin Walker

    Very useful and quick solution. Perfect for what I was looking for… Clears the value like a charm.

  • http://www.ligaweb.ro liga webdesign

    is a common mistake in the contact forms for submitting sites

  • Anon

    Thank you very much!

  • Anon

    Thank you very much!

  • guest

    that works great…..however if they click on the box while imputing there info it gets erased….

    so here is my little tweak. -

  • guest

    sorry that did not transfer right..

  • Parentinghelp Us

    thanks man

  • http://dindingmenulisonline-getaufan.blogspot.com/ getaufan

    great, thanks. oh everyone, please visit my blog http://dindingmenulisonline-getaufan.blogspot.com

Powered by WordPress | Privacy Policy | Web Design Miami