Web Design Blog

This is where we store some of our Web Development thoughts, tips and tricks, just because we like to share.

HTML Form Effect- Remove Default Value On Focus

This Javascript form effect basically removes the default value (e.g Your Name) when the input field is on focus. If nothing is entered, and focus is taken away from the input field, the value is displayed again.

It’s a pretty cool effect, which I’m always using on a lot of projects. Here’s an example (click in the input field, and then click outside the input field):

1
2
3
<form>
<input type="text" value="Your Name" name="name" onblur="if (this.value == '') {this.value = 'Your Name';}"  onfocus="if (this.value == 'Your Name') {this.value = '';}" />
</form>

04 Dec 2008 / 10 Comments / Javascript / by Maruf

10 Comments

  1. John
    23/07/2009
    1

    dude, I was just looking for this and your post saved the day, thx!

  2. Alex
    25/03/2010
    2

    This doesn’t work in IE7

  3. Chaz
    01/07/2010
    3

    Big help, thanks.

  4. David Hernandez
    11/08/2010
    4

    Thanks Maruf. This is exactly what I was looking for, and simpler than I expected.

  5. 5

    Works like a charm, thanks! I’m using it in the comments section of our blog.

  6. chinmayee
    21/02/2011
    6

    helped me a lot.. bt i have one doubt.. if someone click on submit without writing anything then it will take the default value which is not required to do so.. what’s the solution of that?

  7. Vivek
    05/04/2011
    7

    Thanks dude !!! Works exactly the way I wanted….

  8. fatso
    05/05/2011
    8

    how do you get it so that the default text is grey but when you type it types in black?
    Can you email the extension to this code to my Email. Apart from this the ‘remove default value on focus’ form has been very successful. So Cheers.
    The fatso

  9. jassi
    07/10/2011
    9

    how can i apply this into textarea, becuase ther is no value option inside the textarea….?

  10. 25/11/2011
    10

    the placeholder option does the exact same thing. just add placeholder=”Your Name”

Leave a Reply

© 2012 BrightCherry :)