Clearing fields with javascript

November 3rd, 2006  |  Published in Code, Javascript, Technology  |  5 Comments

Here is an easy-to-use javascript that I wrote. It clears a field (input) if a specified word is there. I use this to clear “Search” from the search field at Desiring God. Hopefully it will be of use to others as well:

[source:javascript]
/************************************************************
** Clears a field
** HTML:
** id="search" size="25"
** onFocus="clearField('search', 'Search')" />
***********************************************************/
function clearField(field_id, term_to_clear) {
if (document.getElementById(field_id).value == term_to_clear ) {
document.getElementById(field_id).value = ”;
}
} // end clearField()
[/source]

Like what you see? Subscribe to the RSS feed.

Responses

  1. JC says:

    November 7th, 2006 at 8:30 pm (#)

    Do you have a lot of techies reading this blog?

  2. Josh Sowin says:

    November 8th, 2006 at 12:13 am (#)

    I’m not sure. I think it is a mix. My most popular post is an article on CSS and rounded corners from two years ago, so there are some techies. But I wanted to make it available for google searches too.

  3. AJ says:

    August 26th, 2008 at 1:06 am (#)

    Above script was not working, so some reason.
    I downloaded a .js which does just that, the file is 100K+, a big drawback!!

  4. private says:

    March 6th, 2009 at 11:07 am (#)

    script works like a charm.
    many thanx.

  5. Michael says:

    February 3rd, 2010 at 4:07 am (#)

    Works great, after all these years ;-)

Leave a Response