Clear input field on focus and vice versa

May 24, 2011 at 3:54 pm Leave a comment

Context:

Web forms are popular… very popular. Occasionally, the need arises to have an input field display text by default, but then remove it on focus. In other words, when touched by the cursor.

For example, a field can show the message, “Enter email to subscribe”… and then remove it automatically when the user wants to type in it.

Solution:

1. Use the following javascript

<script language="javascript">
     <!--
     function ClearForm() {
         document.subscription.email.value= "";
     }

     function FillForm() {
         if ( document.subscription.email.value == "" )
         {
             document.subscription.email.value=
                          "Enter email to subscribe.";
         }
     }
     //-->
</script>

2. Coupled with the following form

<form action="" method="post" name="subscription">
    <input name="email" type="text"  class="keywords"
         value="Enter email to subscribe."
         onfocus="ClearForm();" onblur="FillForm();"
    />
    <input name="" type="image" src="images/submit.gif" />
</form>

And that’s all to it. Simple don’t you think !?

Alhumdulillah.

Advertisement

Entry filed under: HTML, Javascript, XHTML. Tags: , , .

“Windows Desktop Gadgets has stopped working” – Windows 7 (x64) How to sync Google Calendar with Thunderbird

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


The Author

A meticulous freelance designer & developer based in Hong Kong with expertise in web development, print design, and search engine optimisation.

Availability

Now available for work !

Contact me for a free consultation / quotation at aziz(at)ibnaziz.hk.

Recent Posts

Categories

Blog Stats

  • 101,934 hits

Follow

Get every new post delivered to your Inbox.