Community Documentation

Clearing a field's default text

Last updated February 1, 2009. Created by joachim on February 1, 2009.
Log in to edit this page.

It's often useful to have default text in a field. For example search boxes that say "Enter some search terms", or example text. But selecting this text to overwrite it is a pain for the user.

Here is a snippet of jquery to blank a field on first click (and then not blank it any more, obviously).

/**
* Clears field texts on click.
*/
$(document).ready(function() {
  $('YOUR SELECTOR HERE').click(function () {
    $(this).attr('value', '');
    $(this).unbind('click');
  }); 
});

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 5.x, Drupal 6.x
Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here