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]
November 7th, 2006 at 8:30 pm (#)
Do you have a lot of techies reading this blog?
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.
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!!
March 6th, 2009 at 11:07 am (#)
script works like a charm.
many thanx.
February 3rd, 2010 at 4:07 am (#)
Works great, after all these years ;-)