UltraMega Blog

Default Form Values with jQuery

Here's a useful jQuery snippet that clears the default values of form fields on focus and refills them if no text is entered. It uses the attribute called defaultValue which stores the original value of a form field.

$(document).ready(function() {
   $('input[type=text]').focus(function() {
      if($(this).val() == $(this).attr('defaultValue')) {
   .blur(function() {
      if($(this).val().length == 0) {

For example, assume you have a field like this:

<input type="text" value="Search..." />

When the page loads, the text field will have "Search..." filled in. When you focus on it, this text will disappear (assuming it hasn't already been edited by you). When you leave focus without typing anything, the text will reappear.

Posted by Steve

Comments (3) Trackbacks (0)
  1. Thanks, I’m still new to Javascript and jQuery so I was so frustrated with my forms to manually change the values. This snippet is just what I need!

  2. $(this)[0].defaultValue = ‘Search…’
    // …


  3. Hi, You should use .prop(‘defaultValue’) instead of .attr(‘defaultValue’) or it will not works in latest jquery version

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

No trackbacks yet.